Inaccessible QPushButton mystery

萝らか妹 提交于 2019-12-13 04:31:35

问题


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?


回答1:


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?




回答2:


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:


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.




回答4:


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



来源:https://stackoverflow.com/questions/6373743/inaccessible-qpushbutton-mystery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!