Nominate our 2022 Qt Champions!

Odd compiler error on a signal & slot

  • I am trying to pass some data to the main window from a child window before closing the child window, and get the following compiler errors:

    variable or field 'newDataSlot' declared void {line 27 below}

    'changed' was not declared in this scope {line 27 below}

    The connection is defined in main

    @ 1 #include <QtGui/QApplication>
    2 #include "pass.h"

    3 int main(int argc, char *argv[])
    4 {
    5 QApplication a(argc, argv);
    6 pass w;
    7 addNew child;

    8 QObject::connect(&child, SIGNAL(dataChangedSignal(bool)), &w, SLOT(newDataSlot(bool)));


    10 return a.exec();

    I have the following definitions in pass.h

    @11 public:
    12 explicit pass(QWidget *parent = 0);
    13 ~pass();
    14 void readDataFile();
    15 QString name;
    16 QString user;
    17 QString date;
    18 QString spin;
    19 QString fileName;
    20 bool changed;

    21 private slots:
    22 void on_pushButtonQuit_clicked();
    23 void on_comboBox_currentIndexChanged();23
    24 void on_pushButtonNew_clicked();
    25 void on_pushButtonShow_clicked();
    26 void newDataSlot(bool);@

    newDataSlot() is defined in passKeeper as

    @27 void pass::newDataSlot(changed)
    29 {
    30 if (changed)
    31 readDataFile();
    32 }@

    In addNew.h the following signals and slots have been defined:

    @33 private slots:
    34 void on_pushButtonAdd_clicked();
    35 void on_pushButtonFinished_clicked();

    36 signals:
    37 void dataChangedSignal(bool);@

    They are then used in addNew.cpp as follows:

    @38 void addNew::on_pushButtonFinished_clicked()
    39 {
    40 if (addedData)
    41 {
    42 bool changed = true;
    43 emit dataChangedSignal(changed);
    44 }
    45 this->close();
    46 }@

    I don't understand the problem of newDataSlot being declared void. It doesn't return any values, ad I thought that slots were required to be void.

    I also don't understand "changed" not being declare in scope, since it is declared in pass.h on line 20.

    It is quite possible that I don't fully understand the signals and slots concept or all the syntaxes involved, or is it some other dumb thing that
    I have missed that has the compiler confused?

    Can anyone help?

  • Moderators

    Slots do not have to be void

    You lack Q_OBJECT macro in pass.h

    line 27: you have not specified the variable type (should be bool changed)

    the slot is declared private which may cause problems

  • Thanks for the help.

    The problem was in the private declaration of the slot. I didn't have to think about it very hard after you pointed it out why it made no sense to declare it as private.

    BTW, I did have Q_OBJECT included, I just didn't show it in the snippets.

    thx again!

Log in to reply