0

I'm trying to make a function that'll show a widget depending on the QWidget passed to it.

I have:

position_widget = new positionWidget();
timing_widget = new timingWidget();
...

void MainWindow::showScreen(QWidget *w)
{
    ui->screenWidget->layout()->addWidget(w);
    w->show();
}


void MainWindow::doConnects()
{
QObject::connect(buttons_widget, SIGNAL(showPositionScreen_signal()), this, SLOT(showScreen(position_screen)));
QObject::connect(buttons_widget, SIGNAL(showTimingScreen_signal()), this, SLOT(showScreen(timing_screen)));
}

Nothing happens when I click the button and it comes up with 'No such slot MainWindow::ShowScreen(timing_screen)'

easty
  • 35
  • 6
  • 1
    It should be `SLOT(showScreen(QWidget *))` instead – vahancho Jun 02 '16 at 07:58
  • Use Qt Creator's autocomplete to complete signals and slots especially with ole style connects (using SIGNAL and SLOT macros). Prefer new style connects where you get compilation error if you got it wrong. – hyde Jun 02 '16 at 08:00
  • @thuga using a value rather than a type for the slot macro is similar to the question you pointed out but there is also a problem with mismatch of arguments in his signals & slots – tobilocker Jun 02 '16 at 08:28
  • @tobilocker There is a mismatch of parameters in both questions. – thuga Jun 02 '16 at 08:37
  • @thuga In both cases a signal is emitted without any argument and one is trying to connect to a slot with a value in signal-macro. You are right - But i see a difference: In this case the user wants to emit a custom signal, when in the other question the user wants to use `QAbstractButton`'s `clicked()` signal (which i actually cant see any use-case for) – tobilocker Jun 02 '16 at 08:46

1 Answers1

1

If showScreen is declared as Qt Slot in your mainwindow.h like:

private slots:
    void showScreen(QWidget* w);

And your Signals are declared in buttons_widget

signals:
    void showPositionScreen_signal(QWidget* w); //Note that signal needs same type as slot
    void showTimingScreen_signal(QWidget* w);

Then you can connect that signal to a slot. Note that the arguments of signals and slots have to match. I.e: "The signals and slots mechanism is type safe: The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)"

connect(buttons_widget, SIGNAL(showPositionScreen_signal(QWidget*)), this, SLOT(showScreen(QWidget*)));

And you will have to emit position_screen and timing_screen from within buttons_widget like:

emit showPositionScreen_signal(position_screen);

As thuga pointed out, it is to say that you do NOT need two different signals. To pass another QWidget to the same slot simply emit that signal with it. I.e.:

emit showPositionScreen_signal(timing_screen);

And i would suggest to change the name of your signal to something appropriate then.

tobilocker
  • 891
  • 8
  • 27