Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I access widgets from another class?
Forum Updated to NodeBB v4.3 + New Features

How do I access widgets from another class?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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 :)

      1 Reply Last reply
      4
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        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?

        mrjjM 1 Reply Last reply
        0
        • ? A Former User

          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?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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

          1 Reply Last reply
          3
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • ? A Former User

              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

              T Offline
              T Offline
              Tirupathi Korla
              wrote on last edited by
              #6

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

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved