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++
QtWS25 Last Chance

Incrementing a number in qt c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 3.5k 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.
  • L Offline
    L Offline
    Lasith
    wrote on 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++;
    }

    jsulmJ 1 Reply Last reply
    0
    • L Lasith

      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++;
      }

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      1
      • jsulmJ jsulm

        @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 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” :(

        jsulmJ 1 Reply Last reply
        0
        • L Lasith

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          5
          • jsulmJ jsulm

            @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 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

            jsulmJ 1 Reply Last reply
            0
            • L Lasith

              @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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              3
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                1
                • SGaistS SGaist

                  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 last edited by
                  #8

                  @SGaist 5.2

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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 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 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
                      • jsulmJ jsulm

                        @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 last edited by
                        #11
                        This post is deleted!
                        mrjjM 1 Reply Last reply
                        0
                        • L Lasith

                          This post is deleted!

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

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

                          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