Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Connected but doesn't work

I have problem with one class in my project, after click appears new window with QTableWidget and QPushButton, after clicking the button I should have "test" on stdout, but nothing shows, here are parts of this code:

Header:

class ClientsSelector : public QWidget {
Q_OBJECT

public:
ClientsSelector(InvoiceTab* parent);
QWidget *window;

private:
QPushButton *accept;

public slots:
void loadData();

Constructor:

window = new QWidget();
layout = new QGridLayout();
layout->addWidget(table, 0, 0);

/*code*/

accept = new QPushButton(QString::fromUtf8("Load data"));
connect(accept, SIGNAL(clicked()), this, SLOT(loadData()));
layout->addWidget(accept, 0, 1);

/*code*/

window->setLayout(layout);

window->show();

Method:

void ClientsSelector::loadData() {

QTextStream std(stdout);
std << "test" << endl;

}

I have not even one warning nor error. I have nothing on stdout, it looks like button was connected to wrong object(?)

like image 505
agilob Avatar asked May 08 '26 18:05

agilob


1 Answers

How do you instantiate ClientsSelector? Isn't it a singleton or global variable by chance? Try moving the connect call to a separate init function which is called after the ClientsSelector constructor. It helped me in similar WTF situations. It has something to do with the fact that each QObject inheritor has a static metadata member and you can't be sure about when it is fully initialized until the constructor finishes. connect won't work without that metadata.

See for example here: http://www.qtcentre.org/threads/9479-connect-in-constructor

If still lost, go through this checklist. Qt signals are so easy to use, everybody sometimes forgets it also has some requirements.

like image 183
Pavel Zdenek Avatar answered May 11 '26 13:05

Pavel Zdenek