Reputation: 969
I need to connect a QPushButton (startListeningPushButton) from my StartWindow to a slot in my MainController. I still have a few questions:
Should I make a pointer of Ui::startWidget ui
, because by default Qt created it as a normal variable?
Is getStartWindow()
the right way to get the StartWindow
from ViewController
?
What would be the right way to get startListeningPushButton
from StartWindow
(is my getter right)?
This is my code:
MainController.cpp:
MainController::MainController()
{
connect(m_viewController.getStartWindow()->getStartListeningPushButton, &QPushButton::clicked, this, &MainController::bla)
}
ViewController.cpp:
StartWindow* ViewController::getStartWindow()
{
return &startWindow;
}
StartWindow.cpp:
QPushButton* StartWindow::getStartListeningPushButton()
{
return ui->fStartListeningPushButton;
}
StartWindow.h:
#ifndef STARTWINDOW_H
#define STARTWINDOW_H
#include "ui_startwindow.h"
class StartWindow : public QWidget
{
Q_OBJECT
public:
StartWindow(QWidget *parent = 0);
~StartWindow();
QPushButton* getStartListeningPushButton();
private:
Ui::startWidget *ui;
};
#endif // STARTWINDOW_H
Upvotes: 1
Views: 58
Reputation: 1047
If you are using Qt Designer and Qt IDE generated such code that it's object not a pointer I don't think that you should make it pointer.
Yeah, returning a pointer to QWidget (StartWindow in your case) is pretty OK.
Seems like you have mistype in your connect, it should look like this:
QObject::connect(m_viewController.getStartWindow()->getStartListeningPushButton(), SIGNAL(clicked()),
this, SLOT(bla()));
Upvotes: 1
Reputation: 21514
It's unclear if you have and then what is your problem.
The only thing I doubt would work is the first parameter of your call to connect
:
m_viewController.getStartWindow()->getStartListeningPushButton
should actually be m_viewController.getStartWindow()->getStartListeningPushButton()
(to have the function be called so that you get the pointer to expected QPushButton
and pass it to the connect
function).
In the connect
function:
QObject*
. So this can either be this
pointer (if current class is derived from QObject
), or any class attribute of type QObject*
(ui->fStartListeningPushButton
) or a function call returning QObject*
(m_viewController.getStartWindow()->getStartListeningPushButton()
). But m_viewController.getStartWindow()->getStartListeningPushButton
(with no ()
) does not make sense here).signal
(declared in header file class using signals:
keyword. You don't need implement any code here, you just declare the signal and Qt MOC mechanism implements it silently). Valid syntax for this parameter is &QPushButton::clicked
or SIGNAL(clicked())
(Qt4 syntax, still valid in Qt5).slot
(declared in header file class using slots:
keyword, and implemented by you). Valid syntax for this parameter is &MainController::bla
or SLOT(bla())
(Qt4 syntax, still valid in Qt5).Upvotes: 1