How do I access widgets from another class?



  • Hello,
    since my project has become very complex I'd like to put some functions into different classes (e.g. a .h and .cpp for all functions that manage one specific QListWidget).
    I am not totally new to OOP but a little bit lost here.

    I have made a new Qt-project and put a pushButton and a label in the UI. Then I created a class called SecondClass and added the secondclass.h and secondclass.cpp to the project.
    Now I'd like declare the void on_pushButton_clicked(); function inside the secondclass.h and use it in the secondclass.cpp to edit the label's text.

    I have tried several things but always got compilation errors.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    QObject *uix = Q_NULLPTR;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        uix = this;
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    secondclass.h

    #ifndef SECONDCLASS_H
    #define SECONDCLASS_H
    #include <QMainWindow>
    
    class SecondClass
    {
    public:
        SecondClass();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
    
    
    };
    
    #endif // SECONDCLASS_H
    

    secondclass.cpp

    #include "secondclass.h"
    
    
    SecondClass::SecondClass()
    {
    
    }
    
    void SecondClass::on_pushButton_clicked()
    {
        ui->label->setText("text changed"); //error here
    }
    

    Any idea?


  • Qt Champions 2017

    Hi
    To use signals and slot in a new class it must be a QObject class, meaning it must have that as baseclass
    and also contain Q_OBJECT macro.

    So to use signals/slots in your class it must be

    class SecondClass : public QObject {
    Q_OBJECT
    ...

    make sure to clean build folder and do build all after adding it .


    void SecondClass::on_pushButton_clicked()
    {
    ui->label->setText("text changed"); //error here
    }

    Second Class do not have an UI, so what are you trying here ?
    do you mean, UI from mainwin ?

    For that you should use signal & slots.
    like let SecondClass emit signal NewText(text)
    and have that hooked up in mainwin to a SLOT
    that uses the text.
    SecondClass should not directly fiddle with other class UI :)



  • Thank you =)
    I have cleaned the build and ran qmake.
    And yes, by ui->label->setText("text changed"); I meant to access the widget of the mainwindow.ui.

    I have updated the class now:

    class SecondClass
    {
        Q_OBJECT
    

    How do I hook up to the mainwindow now?


  • Qt Champions 2017

    @SnuggleKat

    Well you define a new signal.
    like

    signals:
    void MyNewTxt(QString x ) ;

    and then in mainwinow

    you connect an instance of SecondClass
    to some slot in mainwin

    like
    connect( SECONDCLASSINSTANC, SIGNAL(MyNewTxt(QString), this, SLOT( SOMESLOT IN MAINWIN) );

    then in
    SecondClass you can
    do
    emit MyNewTxt("HELLO");

    and it will be sent to mainwins slot



  • By using

    signals:
        void new_text(QString x);
    

    I get the following error: :-1: Fehler: [debug/moc_secondclass.cpp] Error 1

    same when I ass Q_OBJECT to the class



  • @SnuggleKat you have to inherit second class from qobject..
    ‘’’class SecondClass:public QObject’’’


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.