4

I am doing some simple program in Qt (MSVC++2008) with few checkboxes and buttons. In debug mode, everything works fine, but I can't distribute such executable, because most people don't have Visual Studio installed. When I do compile it in release mode, only 2 pushbuttons work.

I have designed my window using Qt Creator's 'drawing tool' (Qt Designer, I guess). I do have such slots defined in my header file:

private slots:
    void on_goButton_clicked(); // Works fine

    void on_InputCheckBox_stateChanged(int arg1); // Don't work
    void on_outputFileCheckBox_stateChanged(int arg1); // Same as above

    void on_inputBrowseButton_clicked();  // Don't work, since theyre disabled
    void on_outputBrowseButton_clicked(); // Same as above

    void replyFinished(QNetworkReply *);

My implentation of those signals looks like this:

void MainWindow::on_InputCheckBox_stateChanged(int arg1)
{
    if (arg1 == Qt::Checked)
    {
        ui->inputEdit->setEnabled(true);
        ui->inputBrowseButton->setEnabled(true);
     }
    else
    {
        ui->inputEdit->setEnabled(false);
        ui->inputBrowseButton->setEnabled(false);
    }
}

void MainWindow::on_outputFileCheckBox_stateChanged(int arg1)
{
    if (arg1 == Qt::Checked)
    {
        ui->outputEdit->setEnabled(true);
        ui->outputBrowseButton->setEnabled(true);
    }
    else
    {
        ui->outputEdit->setEnabled(false);
        ui->outputBrowseButton->setEnabled(false);
    }
}

void MainWindow::on_inputBrowseButton_clicked()
{
    QString documents = DesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
    QString filename = QFileDialog::getOpenFileName(
            this,
            tr("Select input file"),
            documents,
            tr("Text files (*.txt);;All files (*)"));
    if (filename.size() == 0)
        return;
    else
         ui->inputEdit->setText(filename);
}

void MainWindow::on_outputBrowseButton_clicked()
{
    QString documents = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
    QString filename = QFileDialog::getOpenFileName(
            this,
            tr("Select output file"),
            documents,
            tr("Text files (*.txt);;All files (*)"));
    if (filename.size() == 0)
        return;
    else
        ui->inputEdit->setText(filename);
}

Pardon for my English and thank you in advance.

KarolisL
  • 349
  • 1
  • 10
  • 1
    To help with debugging, what happens if you explicitly call connect in your constructor instead of using the auto signal/slot feature? `connect( ui->InputCheckBox, SIGNAL(statechanged(int)), this, SLOT(on_InputCheckBox_stateChanged(int)) )` ? – Chris Oct 07 '11 at 15:28
  • I get `Object::connect: No such slot MainWindow::on_InputCheckBox_stateChanged(int) Object::connect: (sender name: 'InputCheckBox') Object::connect: (receiver name: 'MainWindow')` as an application output. P.S. Thank you for explaining how to explicitly connect to ui objects. :-) – KarolisL Oct 07 '11 at 16:07
  • Sounds like your moc files might not be getting generated properly, try rerunning qmake along with touching your MainWindow header file. – Chris Oct 07 '11 at 16:52

2 Answers2

1

Are you sure the slots aren't being called - or simply not doing what you expect?

The most common reason for 'it works in debug but not release' is unitialised variables. In debug mode the variables are generally set to some null/zero value but not in release mode.

Another common problem are debug macros with side effects. Have you (or some lib) rewritten the connect() call as a macro for debugging and it's doing something different in release?

Time for some printf() debugging ?

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • They are not being called. I've tried adding `qDebug() << "Something";` inside those slots, but nothing happens. It is weird, because 2 buttons - one for quiting and one for 'doing stuff' works. I am using only , and simplecrypt [link](http://developer.qt.nokia.com/wiki/Simple_encryption), but I think none of them rewrites connect(), am I right? And I do not see any unitialized variables in my code. I am using – KarolisL Oct 07 '11 at 16:12
  • Does qDebug() output anything in release mode? In windows IIRC it writes to the system debugger. – Martin Beckett Oct 07 '11 at 16:22
  • It does. If I add `qDebug() << "Something";` in my MainWindow constructor, it outputs me "Something". I do start my program from an IDE - Qt Creator. – KarolisL Oct 07 '11 at 16:24
  • Today I had a similar issue, turned out to be an uninitialised `std::atomic_int` that in debug mode gets set to zero somehow, but not on release. – Darien Pardinas May 31 '17 at 02:19
1

Your code looks good.

Try running "make clean" or "qmake". Your "ui_" or "moc_" files might need updating.

Wes
  • 4,781
  • 7
  • 44
  • 53
  • Well, I have solved my issue following Chris instructions bove my first post. I don't have an ability to make it 'solved' until 8h will pass since my question tmesamp. in that case, I will make your answer as correct since it solves the problem. Thank you for a reply. Anyway, thank for all people who participated in this question, I hope it will be helpful for other people, who will be searching for similar mistakes. Again, thank you. – KarolisL Oct 07 '11 at 23:12