Inaccessible QPushButton mystery

2019-09-06 10:27发布

I have a QT application. It has functioning QPushButtons that are connected to slots in my program. I am now trying 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?

4条回答
Summer. ? 凉城
2楼-- · 2019-09-06 11:21

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)
查看更多
叛逆
3楼-- · 2019-09-06 11:26

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.

查看更多
Anthone
4楼-- · 2019-09-06 11:29

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?

查看更多
萌系小妹纸
5楼-- · 2019-09-06 11:29

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

查看更多
登录 后发表回答