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. Incrementing a number in qt c++
Forum Update on Tuesday, May 27th 2025

Incrementing a number in qt c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 3.7k Views 2 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.
  • L Offline
    L Offline
    Lasith
    wrote on 31 Aug 2017, 09:47 last edited by Lasith
    #1

    In my code I need to increment an integer used by 2 methods(button clicks)! 1 method is used to display the integer(starting value is 1) in a line edit when a button is clicked! The other button is used to increment the number! I need to declare the integer in the header file to be used by the two methods! But in the header file we can declare only static constant so when I increment the error I get an error! How can I avoid this?

    void MainWindow::on_pushButton_2_clicked()
    {
    ui->lineedit->text(QString::number(i));
    }
    void MainWindow::on_pushButton_2_clicked(){
    i++;
    }

    J 1 Reply Last reply 31 Aug 2017, 09:51
    0
    • L Lasith
      31 Aug 2017, 09:47

      In my code I need to increment an integer used by 2 methods(button clicks)! 1 method is used to display the integer(starting value is 1) in a line edit when a button is clicked! The other button is used to increment the number! I need to declare the integer in the header file to be used by the two methods! But in the header file we can declare only static constant so when I increment the error I get an error! How can I avoid this?

      void MainWindow::on_pushButton_2_clicked()
      {
      ui->lineedit->text(QString::number(i));
      }
      void MainWindow::on_pushButton_2_clicked(){
      i++;
      }

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 31 Aug 2017, 09:51 last edited by
      #2

      @Lasith Just declare it as member variable in MainWindow class:

      class MainWindow
      {
          ...
      private:
          int i;
      }
      

      Note: there is no such thing as "qt c++".

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

      L 1 Reply Last reply 31 Aug 2017, 09:58
      1
      • J jsulm
        31 Aug 2017, 09:51

        @Lasith Just declare it as member variable in MainWindow class:

        class MainWindow
        {
            ...
        private:
            int i;
        }
        

        Note: there is no such thing as "qt c++".

        L Offline
        L Offline
        Lasith
        wrote on 31 Aug 2017, 09:58 last edited by Lasith
        #3

        @jsulm Thanx but I get the error message "only static const integral data members can be initialized within a class” :(

        J 1 Reply Last reply 31 Aug 2017, 10:00
        0
        • L Lasith
          31 Aug 2017, 09:58

          @jsulm Thanx but I get the error message "only static const integral data members can be initialized within a class” :(

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 31 Aug 2017, 10:00 last edited by jsulm
          #4

          @Lasith I guess you did it like this?

          class MainWindow
          {
              ...
          private:
              int i = 0;
          }
          

          This is supported in C++11 and newer.
          Do it like this:

          class MainWindow
          {
              ...
          private:
              int i;
          }
          
          // in mainwindow.cpp
          MainWindow::MainWindow(): i(0)
          {
          }
          

          This are C++ basics, you should really take some time to learn C++.
          As alternative you can enable C++11 support (will only work if your compiler supports it!). To do so add this to your pro file:

          CONFIG += c++11
          

          Run qmake and rebuild.

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

          L 1 Reply Last reply 31 Aug 2017, 10:06
          5
          • J jsulm
            31 Aug 2017, 10:00

            @Lasith I guess you did it like this?

            class MainWindow
            {
                ...
            private:
                int i = 0;
            }
            

            This is supported in C++11 and newer.
            Do it like this:

            class MainWindow
            {
                ...
            private:
                int i;
            }
            
            // in mainwindow.cpp
            MainWindow::MainWindow(): i(0)
            {
            }
            

            This are C++ basics, you should really take some time to learn C++.
            As alternative you can enable C++11 support (will only work if your compiler supports it!). To do so add this to your pro file:

            CONFIG += c++11
            

            Run qmake and rebuild.

            L Offline
            L Offline
            Lasith
            wrote on 31 Aug 2017, 10:06 last edited by
            #5

            @jsulm Thanx but where is

            MainWindow::MainWindow():
            {
            }

            to put i(0) ?

            I only find

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)

            in my mainwindow.cpp

            J 1 Reply Last reply 31 Aug 2017, 10:07
            0
            • L Lasith
              31 Aug 2017, 10:06

              @jsulm Thanx but where is

              MainWindow::MainWindow():
              {
              }

              to put i(0) ?

              I only find

              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)

              in my mainwindow.cpp

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 31 Aug 2017, 10:07 last edited by
              #6

              @Lasith Just extend the existing constructor:

              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow),
              i(0)
              {
              ...
              }
              

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

              L 2 Replies Last reply 31 Aug 2017, 10:09
              3
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 31 Aug 2017, 10:08 last edited by
                #7

                Hi,

                To add to @jsulm which version of Qt are you using ? Since 5.7, C++11 is mandatory.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                L 1 Reply Last reply 31 Aug 2017, 10:08
                1
                • SGaistS SGaist
                  31 Aug 2017, 10:08

                  Hi,

                  To add to @jsulm which version of Qt are you using ? Since 5.7, C++11 is mandatory.

                  L Offline
                  L Offline
                  Lasith
                  wrote on 31 Aug 2017, 10:08 last edited by
                  #8

                  @SGaist 5.2

                  1 Reply Last reply
                  0
                  • J jsulm
                    31 Aug 2017, 10:07

                    @Lasith Just extend the existing constructor:

                    MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow),
                    i(0)
                    {
                    ...
                    }
                    
                    L Offline
                    L Offline
                    Lasith
                    wrote on 31 Aug 2017, 10:09 last edited by
                    #9

                    @jsulm Thanx for your great help! I am new to Qt and c++ programming

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 31 Aug 2017, 10:11 last edited by
                      #10

                      Then please consider upgrading to a more recent version, we are currently at Qt 5.9.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • J jsulm
                        31 Aug 2017, 10:07

                        @Lasith Just extend the existing constructor:

                        MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow),
                        i(0)
                        {
                        ...
                        }
                        
                        L Offline
                        L Offline
                        Lasith
                        wrote on 31 Aug 2017, 10:12 last edited by
                        #11
                        This post is deleted!
                        mrjjM 1 Reply Last reply 31 Aug 2017, 10:13
                        0
                        • L Lasith
                          31 Aug 2017, 10:12

                          This post is deleted!

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 31 Aug 2017, 10:13 last edited by mrjj
                          #12

                          @Lasith
                          thats just
                          i=0;
                          in any function that is member of the class.

                          1 Reply Last reply
                          2

                          1/12

                          31 Aug 2017, 09:47

                          • Login

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