3

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

agilob
  • 6,082
  • 3
  • 33
  • 49

3 Answers3

1

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.

Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
0

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

Try using

qDebug() << "test";

instead of QTextstream

Kotte
  • 811
  • 1
  • 9
  • 21
  • 1
    I made a typo here in the post, in my editor i have `"test"` Edit: Still nothing – agilob Sep 13 '12 at 10:36
  • 1
    The code seems correct to me. Are you sure that connect() line is run? Try adding a printout there as well and check that `accept` is not `NULL` or something like that. – Kotte Sep 13 '12 at 10:39
  • 1
    Does this run in a multithreaded program? There are some issues when running multiple threads. You sometimes need to call `exec()` in your thread to activate the event loop. – Kotte Sep 13 '12 at 10:49
  • Have you started the event loop in your `main()` `QCoreApplication app(argc, argv); app.exec();` – Kotte Sep 13 '12 at 11:11
  • As I wrote in the top post, this is a child windows which appears after clicking a button from parent window. I have this. – agilob Sep 13 '12 at 11:16
0

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.

dev
  • 2,180
  • 2
  • 30
  • 46