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. Accessing label from function gives error
Forum Updated to NodeBB v4.3 + New Features

Accessing label from function gives error

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 799 Views
  • 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.
  • I Offline
    I Offline
    Imran Hassan
    wrote on last edited by
    #1

    Hi everyone,

    I am new to QT. I created an application by a wizard. Its UI backend was created as below.

    MainWindow::MainWindow(QWidget *parent)
    :QmainWindow(parent)
    ,ui(new UI::MainWindow) {
    }

    I placed a label on UI with name lbl. I created a timer and a function called myFunc() to update the time on label (lbl). I want to update the label (lbl) with time on every 1 sec tick of timer. I connected the timer signal with slot (myFunc). In myFunc I want to access the label to update the text with correct time but it gives me error.

    I want to know two things, in this auto creation of MainWindow class how can I declare private and public data and member functions and second how can I access this lbl from myFunc().

    Help will be appreciated.

    jsulmJ 1 Reply Last reply
    0
    • I Imran Hassan

      Hi everyone,

      I am new to QT. I created an application by a wizard. Its UI backend was created as below.

      MainWindow::MainWindow(QWidget *parent)
      :QmainWindow(parent)
      ,ui(new UI::MainWindow) {
      }

      I placed a label on UI with name lbl. I created a timer and a function called myFunc() to update the time on label (lbl). I want to update the label (lbl) with time on every 1 sec tick of timer. I connected the timer signal with slot (myFunc). In myFunc I want to access the label to update the text with correct time but it gives me error.

      I want to know two things, in this auto creation of MainWindow class how can I declare private and public data and member functions and second how can I access this lbl from myFunc().

      Help will be appreciated.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Imran-Hassan said in Accessing label from function gives error:

      it gives me error

      What error?!

      "how can I declare private and public data and member functions and second how can I access this lbl from myFunc()" - C++ basics? Is myFunc member of your MainWindow?

      You should really post your MainWindow code...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Imran-Hassan said in Accessing label from function gives error:

        it gives me error

        What error?!

        "how can I declare private and public data and member functions and second how can I access this lbl from myFunc()" - C++ basics? Is myFunc member of your MainWindow?

        You should really post your MainWindow code...

        I Offline
        I Offline
        Imran Hassan
        wrote on last edited by
        #3

        Let me paste the full code. In TimerSlot() function it is giving error "Use of undeclared identifier ui"

        QTimer *timer; // NEW
        void TimerSlot(); // NEW slot
        MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)

        , ui(new Ui::MainWindow)
        

        {
        //saveSetting();
        loadSettings();
        ui->setupUi(this);
        // this->layout()->setSizeConstraint(QLayout::SetFixedSize);
        const QString time = QDateTime::currentDateTime().toString();
        ui->currentDateTime->clear();
        ui->currentDateTime->setText(time);
        timer = new QTimer(this); // create it
        connect(timer, &QTimer::timeout, this, TimerSlot); // connect it
        timer->start(1000); // 1 sec timer
        }

        void TimerSlot()
        {

        ui->lbl.setText(QDateTime::currentDateTime().toString());
        }

        jsulmJ JonBJ 2 Replies Last reply
        0
        • I Imran Hassan

          Let me paste the full code. In TimerSlot() function it is giving error "Use of undeclared identifier ui"

          QTimer *timer; // NEW
          void TimerSlot(); // NEW slot
          MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)

          , ui(new Ui::MainWindow)
          

          {
          //saveSetting();
          loadSettings();
          ui->setupUi(this);
          // this->layout()->setSizeConstraint(QLayout::SetFixedSize);
          const QString time = QDateTime::currentDateTime().toString();
          ui->currentDateTime->clear();
          ui->currentDateTime->setText(time);
          timer = new QTimer(this); // create it
          connect(timer, &QTimer::timeout, this, TimerSlot); // connect it
          timer->start(1000); // 1 sec timer
          }

          void TimerSlot()
          {

          ui->lbl.setText(QDateTime::currentDateTime().toString());
          }

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Imran-Hassan said in Accessing label from function gives error:

          void TimerSlot()

          Shouldn't it be

          void MainWindow::TimerSlot()
          

          ?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • I Imran Hassan

            Let me paste the full code. In TimerSlot() function it is giving error "Use of undeclared identifier ui"

            QTimer *timer; // NEW
            void TimerSlot(); // NEW slot
            MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)

            , ui(new Ui::MainWindow)
            

            {
            //saveSetting();
            loadSettings();
            ui->setupUi(this);
            // this->layout()->setSizeConstraint(QLayout::SetFixedSize);
            const QString time = QDateTime::currentDateTime().toString();
            ui->currentDateTime->clear();
            ui->currentDateTime->setText(time);
            timer = new QTimer(this); // create it
            connect(timer, &QTimer::timeout, this, TimerSlot); // connect it
            timer->start(1000); // 1 sec timer
            }

            void TimerSlot()
            {

            ui->lbl.setText(QDateTime::currentDateTime().toString());
            }

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Imran-Hassan

            void TimerSlot()

            This is simply a global function. You need it to be a member of MainWindow, at least if you expect it to access ui.

            I 1 Reply Last reply
            1
            • JonBJ JonB

              @Imran-Hassan

              void TimerSlot()

              This is simply a global function. You need it to be a member of MainWindow, at least if you expect it to access ui.

              I Offline
              I Offline
              Imran Hassan
              wrote on last edited by
              #6

              @jsulm and @JonB Yes it should be but in this auto-created code, I am not able to know how to declare public and private member functions and data.

              Can you guide me in this particular scenario? I mean using this above code. I can if I create new application and declare my own class of MainWindow but I have already done a lot of work to create GUI so I want to fix in this code.

              Secondly this QT is now allowing me to reply within 10 minutes without earning a reputation.

              JonBJ I 2 Replies Last reply
              0
              • I Imran Hassan

                @jsulm and @JonB Yes it should be but in this auto-created code, I am not able to know how to declare public and private member functions and data.

                Can you guide me in this particular scenario? I mean using this above code. I can if I create new application and declare my own class of MainWindow but I have already done a lot of work to create GUI so I want to fix in this code.

                Secondly this QT is now allowing me to reply within 10 minutes without earning a reputation.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @Imran-Hassan said in Accessing label from function gives error:

                Yes it should be but in this auto-created code, I am not able to know how to declare public and private member functions and data.

                The auto-generated code goes into a file named ui_mainwindow.h in your debug/release directory. This is not a file you see or edit in Creator, and you should not edit this file. You have your own mainwindow.cpp/.h which #includes that file. There you declare your own MainWindow class which inherits from Ui::MainWindow. This file is generated initially by Creator, but thereafter is not altered. You can & should make your own changes in your own mainwindow.cpp/.h files.

                I 1 Reply Last reply
                2
                • I Imran Hassan

                  @jsulm and @JonB Yes it should be but in this auto-created code, I am not able to know how to declare public and private member functions and data.

                  Can you guide me in this particular scenario? I mean using this above code. I can if I create new application and declare my own class of MainWindow but I have already done a lot of work to create GUI so I want to fix in this code.

                  Secondly this QT is now allowing me to reply within 10 minutes without earning a reputation.

                  I Offline
                  I Offline
                  Imran Hassan
                  wrote on last edited by
                  #8

                  @Imran-Hassan I got the answer .... To help other here is the answer

                  can do that in the mainwindow.h file.

                  class MainWindow {
                  //...
                  private slots:
                  void TimerSlot();
                  };

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Imran-Hassan said in Accessing label from function gives error:

                    Yes it should be but in this auto-created code, I am not able to know how to declare public and private member functions and data.

                    The auto-generated code goes into a file named ui_mainwindow.h in your debug/release directory. This is not a file you see or edit in Creator, and you should not edit this file. You have your own mainwindow.cpp/.h which #includes that file. There you declare your own MainWindow class which inherits from Ui::MainWindow. This file is generated initially by Creator, but thereafter is not altered. You can & should make your own changes in your own mainwindow.cpp/.h files.

                    I Offline
                    I Offline
                    Imran Hassan
                    wrote on last edited by
                    #9

                    @JonB Thanks a lot dear.. For such nice help. Can you upvote this question so that I can gain reputation to ask questions frequently

                    Anyway thanks again for such nice help

                    1 Reply Last reply
                    1

                    • Login

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