开发者

Inaccessible QPushButton mystery

I have a QT application. It has functioning QPushButtons that are connected to slots in my program. I am now tryin开发者_如何学Gog to add a new button myNewButton, but somewhere, somehow it is not being registered.

For an existing, working button myExistingButton I have the line:

connect(ui->myExistingButton, SIGNAL(clicked()), this, SLOT(Foo()));

I cannot simply add the line:

connect(ui->myNewButton, SIGNAL(clicked()), this, SLOT(Foo()));

The compilation error is:

class UI::Viewer has no member named 'myNewButton'

And QT Creator does not list the button as an autocomplete option for ui-> (i.e. I haven't simply mis-spelled the name). The records for myExistingButton and myNewButton are identical apart from coordinates and naming in the ui file. What could be causing this problem?


I assume you did add a new button to the .ui form and named it myNewButton, right? If so, this could be a synchronization issue in Qt Creator. Have you tried saving all and rebuilding (just to make sure the ui compiler is triggered) your app?


Have you set the widgets parent-property properly?

Either upon creation, or by explicitly setting it.

edit: Also note that

  • you shouldn't edit ui_-includes directly, use the form-editor instead
  • you don't have to do explicit connects for stuff that is hard-declared in your form; again, use the form-editor for this (right click on the button)


I see you solved your problem by deleting ui_viewer.h. But a cleaner way is to run qMake (from the Build menu). I always do this when I get errors like yours -- sometimes Qt Creator forgets to run qMake when it should.


I fixed this by deleting the generated file ui_viewer.h and then rebuilding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜