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

Qt coding

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.6k 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.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by
    #1

    I like C# for its very clear syntax. Could you please point me out how to write this piece of C# code in Qt to achieve best readability of the code:

    Timer timer = new Timer();
    timer.Elapsed += (sender, e) => { Console.WriteLine("Elapsed");};
    timer.Interval = 100;
    timer.Start();

    I like this code in C# because only 4 lines of code are sufficient to start timer and do some action when the timer elapses. Also I like that you can see all the functionality on one place (anonymous method). I hope something similar is achievable in Qt and C++.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      elveatles
      wrote on last edited by elveatles
      #2

      QTimer has a good example of this in their documentation:

      http://doc.qt.io/qt-4.8/qtimer.html

      There's no anonymous function call, but the syntax is still pretty clear.

      M 1 Reply Last reply
      0
      • jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        One note: Qt is not a programming language! Qt is a C++ framework (similar to what .Net framework is) and as such is usually used with C++ as programming language. But you can use Qt with other programming languages as well, for example Python.

        C++ supports lambda functions since C++11 standard.

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

        M 1 Reply Last reply
        0
        • E elveatles

          QTimer has a good example of this in their documentation:

          http://doc.qt.io/qt-4.8/qtimer.html

          There's no anonymous function call, but the syntax is still pretty clear.

          M Offline
          M Offline
          MartinD
          wrote on last edited by
          #4

          @elveatles So this seems to be minimal syntax, right?

          QTimer *timer = new QTimer(this);
          connect(timer, SIGNAL(timeout()), this, SLOT(elapsed()));
          timer->start(1000);

          // then somewhere there is elapsed method:

          void elapsed( void) {
          qDebug() << "Elapsed"
          }

          // Then in .h file I also need to declare elapsed().

          void elapsed( void);

          1 Reply Last reply
          0
          • E Offline
            E Offline
            elveatles
            wrote on last edited by
            #5

            Yeah, That's the closest Qt translation.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              One note: Qt is not a programming language! Qt is a C++ framework (similar to what .Net framework is) and as such is usually used with C++ as programming language. But you can use Qt with other programming languages as well, for example Python.

              C++ supports lambda functions since C++11 standard.

              M Offline
              M Offline
              MartinD
              wrote on last edited by
              #6

              @jsulm Sorry not to be very precise.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Devopia53
                wrote on last edited by
                #7

                // with c++11

                auto timer = new QTimer;
                connect(timer, &QTimer::timeout, []{qDebug() << "Elapsed";});
                timer->start(1000);

                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