Engo
Engo

Reputation: 969

How do I get ui from a widget in order to connect it in another class?

I need to connect a QPushButton (startListeningPushButton) from my StartWindow to a slot in my MainController. I still have a few questions:

  1. Should I make a pointer of Ui::startWidget ui, because by default Qt created it as a normal variable?

  2. Is getStartWindow() the right way to get the StartWindow from ViewController?

  3. 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

Answers (2)

Pustovalov Dmitry
Pustovalov Dmitry

Reputation: 1047

  1. 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.

  2. Yeah, returning a pointer to QWidget (StartWindow in your case) is pretty OK.

  3. Your getter is 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

jpo38
jpo38

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:

  • First and third parameter must be of type 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).
  • Second parameter must be a 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).
  • Fourth parameter must be a 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).
  • There's actually a fifth optional parameter to use when you'll start dealing with threads.

Upvotes: 1

Related Questions