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 Updated to NodeBB v4.3 + New Features

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.
  • 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