QT Connect Signal Slot and initialize in constructor

痞子三分冷 提交于 2020-01-17 02:58:07

问题


I create connections between widged in the constructor and also initialize them. But the connect not working (inside the methode).

Code says alot:

MyApp::MyApp(QWidget *parent) : QMainWindow(parent)
{
    ui.setupUi(this);

    // slider to spinbox
    connect(ui.slider, SIGNAL(valueChanged(int)), ui.spinbox, SLOT(setValue(int)));

    // SIGNAL To SLOT not called
    ui.slider->setValue(2);
    // I have to set this also:
    ui.spinbox->setValue(2);
}

回答1:


Have you tried doing

connect(ui.slider, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));

and have the slot setValue(int) update the spinbox when the slider is changed?

If you need it to go both ways, you will need to make two connections. One to connect a signal/slot to the slider, and one to connect a signal/slot to the spinbox.

Also, are you getting any errors?



来源:https://stackoverflow.com/questions/24635369/qt-connect-signal-slot-and-initialize-in-constructor

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