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

Qt Beginner

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 1.0k Views 3 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.
  • C Offline
    C Offline
    CleaD
    wrote on last edited by
    #1

    Hello everyone
    I would need your help to write my first application in Qt using C++. to solve this example:

    Write an application that supports two independent tasks, executed in series. The first task should allow the choice of a text, through GUI, that will be printed every t1 seconds. The second task will have to check, every t2 seconds, if a specific file exists. Through the QSystemTrayIcon the user will have the opportunity to interact with the application by accessing all the configurations and parameters required by the two tasks.

    To solve this exercise I'm looking at the example on the QSystemTrayIcon provided by Qt and I think I should use the QDateTime Class. Is this the right approach? Can someone kindly provide me with some basic code lines to get started?

    Thank you :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What do you mean by printed ?
      What will you do if that file exists ?

      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
      • C Offline
        C Offline
        CleaD
        wrote on last edited by
        #3

        Hi SGaist,
        actualy I can write with Qt a simple program that show in console task1 and task2 msg in a periodical way using QTimer obj. This is the code for task1.cpp (called by the main.cpp):

        #include "task1.h"
        #include <QtCore>
        #include <QtDebug>

        Task1::Task1()
        {
        timer1 = new QTimer(this);
        connect(timer1,SIGNAL(timeout()),this,SLOT(MySlot1()));

        timer1->start(1000);
        

        }

        void Task1::MySlot1()
        {
        qDebug() << "Task1 executed";
        }

        I don't know how to connect task1 to a GUI.
        For the task2, there is nothing to do if the file exists, but the name of the file to search is a parameter of the QSystemTrayIcon.

        Pl45m4P 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You have to give these classes a proper API to be able to configure whatever you want on them as well as restart the QTimer.

          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
          0
          • C CleaD

            Hi SGaist,
            actualy I can write with Qt a simple program that show in console task1 and task2 msg in a periodical way using QTimer obj. This is the code for task1.cpp (called by the main.cpp):

            #include "task1.h"
            #include <QtCore>
            #include <QtDebug>

            Task1::Task1()
            {
            timer1 = new QTimer(this);
            connect(timer1,SIGNAL(timeout()),this,SLOT(MySlot1()));

            timer1->start(1000);
            

            }

            void Task1::MySlot1()
            {
            qDebug() << "Task1 executed";
            }

            I don't know how to connect task1 to a GUI.
            For the task2, there is nothing to do if the file exists, but the name of the file to search is a parameter of the QSystemTrayIcon.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @CleaD said in Qt Beginner:

            I don't know how to connect task1 to a GUI.

            In the same way, you connected your timer to your function.
            Design + create the GUI of your choice and then add your two functions (running in different threads?!) to this. First function prints (writes to file?!) the user selected text every second and the second function checks, whether your specific file exists or not.

            Edit:

            For what reason you want to use QDateTime? Everything you described has no need to use QDateTime at all.
            Overall it seem a little bit too theoretical. What do you have so far (except that console example posted above)?
            Start designing your GUI, create your QSystemTrayIcon and try to run your test functions first. If you have problems with a specific step, feel free to ask :)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            C 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @CleaD said in Qt Beginner:

              I don't know how to connect task1 to a GUI.

              In the same way, you connected your timer to your function.
              Design + create the GUI of your choice and then add your two functions (running in different threads?!) to this. First function prints (writes to file?!) the user selected text every second and the second function checks, whether your specific file exists or not.

              Edit:

              For what reason you want to use QDateTime? Everything you described has no need to use QDateTime at all.
              Overall it seem a little bit too theoretical. What do you have so far (except that console example posted above)?
              Start designing your GUI, create your QSystemTrayIcon and try to run your test functions first. If you have problems with a specific step, feel free to ask :)

              C Offline
              C Offline
              CleaD
              wrote on last edited by
              #6

              @Pl45m4 Yes. It is only an example to begin and remember oop programming (quarantine hobby :D).
              It isn’t requested to use thread, but if it is used only one thread is permitted.
              In C I resolved with func pointer, is it right approach?

              Pl45m4P 1 Reply Last reply
              0
              • C CleaD

                @Pl45m4 Yes. It is only an example to begin and remember oop programming (quarantine hobby :D).
                It isn’t requested to use thread, but if it is used only one thread is permitted.
                In C I resolved with func pointer, is it right approach?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @CleaD

                You can pass variables by using Signals & Slots.

                Start with your GUI design and create your QSystemTrayIcon + the menu (a QMenu with QActions), where you can do your stuff and re-open the GUI.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                C 1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @CleaD

                  You can pass variables by using Signals & Slots.

                  Start with your GUI design and create your QSystemTrayIcon + the menu (a QMenu with QActions), where you can do your stuff and re-open the GUI.

                  C Offline
                  C Offline
                  CleaD
                  wrote on last edited by
                  #8

                  @Pl45m4
                  This is my task1:

                  void Task1::MySlot1()
                  {
                  QString text("Task1 is running");
                  QLabel *label = new QLabel(text);
                  label->setWordWrap(true);

                  label->show();
                  

                  }

                  and this is the "bodyEdit" var from Window::createMessageGroupBox() of the app QSystemTrayIcon:

                  void Window::createMessageGroupBox()
                  {
                  ....
                  bodyEdit = new QTextEdit;
                  bodyEdit->setPlainText(tr("Task1 is running (text modified)."));
                  ...
                  }

                  How can I connect "bodyEdit" to "label"?

                  JonBJ 1 Reply Last reply
                  0
                  • C CleaD

                    @Pl45m4
                    This is my task1:

                    void Task1::MySlot1()
                    {
                    QString text("Task1 is running");
                    QLabel *label = new QLabel(text);
                    label->setWordWrap(true);

                    label->show();
                    

                    }

                    and this is the "bodyEdit" var from Window::createMessageGroupBox() of the app QSystemTrayIcon:

                    void Window::createMessageGroupBox()
                    {
                    ....
                    bodyEdit = new QTextEdit;
                    bodyEdit->setPlainText(tr("Task1 is running (text modified)."));
                    ...
                    }

                    How can I connect "bodyEdit" to "label"?

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

                    @CleaD
                    When you go bodyEdit->setPlainText(), the QTextEdit emits a signal textChanged(), as per https://doc.qt.io/qt-5/qtextedit.html#textChanged.

                    You want Task1 to have a slot connect()ed to that signal, https://doc.qt.io/qt-5/qobject.html#connect-1, so that it will be notified when bodyEdit's text is changed.

                    Get that code working, then you'll need to deal with how best to have the slot in Task know what the changed text in bodyEdit now is, if you want to access that.

                    If you are to use Qt, and certainly for your application exercise, you need to read https://doc.qt.io/qt-5/signalsandslots.html to explain signals/slots if you are to accomplish your task. Otherwise you're just asking other people to write the code for you.

                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      CleaD
                      wrote on last edited by
                      #10

                      Thanks to all for the support. I'm reaching the solution deep learning the main exampled offered by the Qt suite: "System Try Icon", "Find File".
                      My last question is if two objects of the same class can be considered as different tasks.

                      Pablo J. RoginaP 1 Reply Last reply
                      0
                      • C CleaD

                        Thanks to all for the support. I'm reaching the solution deep learning the main exampled offered by the Qt suite: "System Try Icon", "Find File".
                        My last question is if two objects of the same class can be considered as different tasks.

                        Pablo J. RoginaP Offline
                        Pablo J. RoginaP Offline
                        Pablo J. Rogina
                        wrote on last edited by
                        #11

                        @CleaD said in Qt Beginner:

                        My last question is if two objects of the same class can be considered as different tasks.

                        Maybe this analogy is applicable to you. Let's say your class is ToPaint i.e. paint some part of the house.

                        One object (one task) is to paint the bedroom, while another object (other different task) is to paint the living room.

                        So you actually have two different tasks, as two painters can work at the same time in the different rooms, while the class is the same (to paint)

                        Upvote the answer(s) that helped you solve the issue
                        Use "Topic Tools" button to mark your post as Solved
                        Add screenshots via postimage.org
                        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                        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