Qt Beginner
-
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 :)
-
Hi and welcome to devnet,
What do you mean by printed ?
What will you do if that file exists ? -
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. -
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.
-
@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 useQDateTime
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 yourQSystemTrayIcon
and try to run your test functions first. If you have problems with a specific step, feel free to ask :) -
-
@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"?
-
@CleaD
When you gobodyEdit->setPlainText()
, theQTextEdit
emits a signaltextChanged()
, as per https://doc.qt.io/qt-5/qtextedit.html#textChanged.You want
Task1
to have a slotconnect()
ed to that signal, https://doc.qt.io/qt-5/qobject.html#connect-1, so that it will be notified whenbodyEdit
'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 inbodyEdit
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.
-
@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)