Question

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(?)

Was it helpful?

Solution

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.

OTHER TIPS

Seems like you forgot a closing " on the test printout.

Try using

qDebug() << "test";

instead of QTextstream

You can do following to make sure that the connection was made and slot is called.

  1. connect function returns status of connection. Check if the connection was made properly.
  2. put a breakpoint and see if the loadData() is called when button is pressed.

This might help to find the cause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top