Does QTimer resuse unique id? - qt

I looked in the documentation and I didn't find something about reusing unique Id.
The documentation says about the startTimer function:
The function returns a unique integer timer ID
But how long will it be unique? Does it reuse the ids at some point?

But how long will it be unique?
The timer ID should remain unique until it is released via QAbstractEventDispatcherPrivate::releaseTimerId();
In other words, calling killTimer().
Does it reuse the ids at some point?
I quickly wrote a simple test to see if the timer ID would get reused:
Something.h:
#include <QCoreApplication>
#include <QtCore>
#include <QtGlobal>
class Something : public QObject
{
Q_OBJECT
public:
Something() : QObject() {}
~Something() {}
};
main.cpp:
#include "Something.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Something thing;
int i = thing.startTimer(1000);
qDebug() << i;
thing.killTimer(i);
i = thing.startTimer(1000);
qDebug() << i;
return a.exec();
}
The result:
1
1
Finally, using QTimer exclusively... you can test with:
class Something : public QObject
{
Q_OBJECT
public:
Something() : QObject(), timer(NULL) {}
~Something() {}
void runme()
{
timer = new QTimer();
// Old school ;)
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent()));
timer->start(100);
}
public Q_SLOTS:
void timerEvent()
{
qDebug() << timer->timerId();
timer->stop();
delete timer;
timer = new QTimer();
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent()));
timer->start(1000);
}
public:
QTimer* timer;
};
Then just kick it off and run it:
Something thing;
thing.runme();

Related

QThread created in other QThread not calling destructor

I have a code:
#include <QApplication>
#include <QThread>
#include <QDebug>
class ChildThread : public QThread {
Q_OBJECT
public:
ChildThread() {
connect(this, &QThread::finished, this, &ChildThread::deleteLater);
}
~ChildThread() {
qDebug() << "~TR()";
}
protected:
void run() {
qDebug() << "RUN";
}
};
class ParentThread : public QThread {
Q_OBJECT
public:
protected:
void run() {
for (int i = 0; i < 10; i++) {
ChildThread *t = new ChildThread();
t->start();
msleep(1000);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ParentThread *t = new ParentThread();
t->start();
for (int i = 0; i < 10; i++) {
ChildThread *t = new ChildThread();
t->start();
QThread::msleep(1000);
}
return a.exec();
}
And I havnt understand, why all of the ChildThread that created in the ParentThread after reaching end of run() function not ending and desctructor of the ChildThread not called, but if I create ChildThread outside of ParentThread, deststructor of ChildThread was called?
Thank You!
The thread will not be destroyed when the run() function due to the
fact that the deleteLater() function is called when the parent (main) loop
get back the control.
Creating the ChildThread from main(), the QThread::msleep() call
give back the control to the main loop, so the destructor can be
called.

Change signal-slot connection in a slot when called

I have a use case when user actions initialise application data at each runtime. To represent that behavior here is my sample code:
SignalSlotChange.h
#ifndef SIGNALSLOTCHANGE_H
#define SIGNALSLOTCHANGE_H
#include <QtGui>
class SignalSlotChange : public QWidget
{
Q_OBJECT
public:
SignalSlotChange(QWidget *parent = 0);
private slots:
void firstCall();
void secondCall();
private:
QPushButton *button;
};
#endif // SIGNALSLOTCHANGE_H
SignalSlotChange.cpp
#include "SignalSlotChange.h"
SignalSlotChange::SignalSlotChange(QWidget *parent) : QWidget(parent)
{
button = new QPushButton("Messgage", this);
QObject::connect(button, SIGNAL(clicked()), this, SLOT(firstCall()));
show();
}
void SignalSlotChange::firstCall()
{
QMessageBox::information(this, "SignalSlotChange", "First call", QMessageBox::Ok, QMessageBox::NoButton);
// Change the signal-slot connection to secondCall()
QObject::disconnect(button, SIGNAL(clicked()), this, SLOT(firstCall()));
QObject::connect(button, SIGNAL(clicked()), this, SLOT(secondCall()));
}
void SignalSlotChange::secondCall()
{
QMessageBox::information(this, "SignalSlotChange", "Second call", QMessageBox::Ok, QMessageBox::NoButton);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SignalSlotChange ssc;
return app.exec();
}
When the button is pressed then the initialising slot firstCall() is called. It changed the signal-slot connection to secondCall() for subsequent signals.
The problem with the way I do it is that it is highly coupled and requires that slot knows exact method and signals to change it.
With QObject::sender() I would know origin of sender but not its signal, and I would know about just one sender which happened to emit that signal.
I can do it with a Boolean first_call but that would be making a check on Boolean value for all subsequent calls, something which I want to avoid and hence this question.
A somewhat different solution could be implemented by using a pointer-to-member approach:
SignalSlotChange.h
#ifndef SIGNALSLOTCHANGE_H
#define SIGNALSLOTCHANGE_H
#include <QtGui>
class SignalSlotChange : public QWidget {
Q_OBJECT
public:
SignalSlotChange(QWidget *parent = 0);
private slots:
void callCall();
private:
void (SignalSlotChange::* delegate) ();
void firstCall();
void secondCall();
QPushButton *button;
};
#endif // SIGNALSLOTCHANGE_H
SignalSlotChange.cpp
#include "SignalSlotChange.h"
SignalSlotChange::SignalSlotChange(QWidget *parent) : QWidget(parent) {
delegate = &SignalSlotChange::firstCall;
button = new QPushButton("Messgage", this);
QObject::connect(button, SIGNAL(clicked()), this, SLOT(callCall()));
show();
}
void SignalSlotChange::callCall() {
(this->*delegate) ();
}
void SignalSlotChange::firstCall() {
QMessageBox::information(this, "SignalSlotChange", "First call", QMessageBox::Ok, QMessageBox::NoButton);
// Change the effective signal-slot connection to secondCall()
delegate = &SignalSlotChange::secondCall;
}
void SignalSlotChange::secondCall() {
QMessageBox::information(this, "SignalSlotChange", "Second call", QMessageBox::Ok, QMessageBox::NoButton);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
SignalSlotChange ssc;
return app.exec();
}
I am not sure if this is better solution from a decoupling perspective, but the advantage is that the mechanism does not need to know anything about the slot which was actually triggered. The complete logic to implement the "method switch" is encapsulated within the SignalSlotChange class. The callCall() slot can be connected to any other compatible signal and the method switch still works, with no further code changes.

Qt creating children threads using QFuture error

I am trying to make a Collaborative Editor(I have to use Linux networking libraries for all the networking stuff), I have the main widget(custom made class that inherits QWidget) with all the components. In the constructor I create all the Widgets on this main Widget and at the end I try to create a new thread using QFuture(I use QFuture instead of QThread cause it allows me easily to call functions with any type of parameters, like QTextEdit, QTextCursor...) but it gives me this error at compilation:
"QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x1b064b0), parent's thread is QThread(0x1985750), current thread is QThread(0x1ae7610)".
How to solve the error?
Here is my code:
mainwindow.h:
...//includes
using namespace QtConcurrent;
...
namespace Ui {
class Widget;
class TextEdit;
}
class TextEdit;
class Widget;
class Widget : public QWidget {
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
...
QFuture<void> thread;
}
class TextEdit : public QTextEdit {
Q_OBJECT
...
}
static void receiveKeyPress(TextEdit *textedit, QTextCursor *secondUserCursor) {
unsigned long long int Number = NULL;
QMessageBox::information(textedit->parentWidget(), "UI Component", "This makes the thread to throw the error");
while(1) if(connected == 1) {
read(recvFileDescriptor, &Number, sizeof(unsigned long long int));
if( Number != NULL)
if( Number == Qt::Key_Home )
secondUserCursor->movePosition(QTextCursor::StartOfLine);
...
else {
QTextCharFormat backgroundFormat = textedit->textCursor().charFormat();
backgroundFormat.setBackground(QColor("lightGreen"));
//If I don't use QMessageBox up there, it breaks here on the next command
secondUserCursor->setCharFormat(backgroundFormat);
secondUserCursor->setPosition(textedit->textCursor().position());
secondUserCursor->insertText(QString::number(Number));
} //else
}//while
}//the function
And mainwindow.cpp:
#include "mainwindow.h"
Widget::Widget(QWidget *parent) {
...
thread = run(receiveKeyPress, this->edit1, this->edit1->secondUserCursor); //run is from QtConcurrent namespace
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget window;
...
window.show();
return a.exec();
}
I've read here on stackoverflow how others use QObject(which I never used and I don't get the idea of it) and QThread(the only combination) but I already tried to use QThread and I wasn't able to pass QTextEdit and QTextCursor to it.
Thanks in advance
Edit:
mainwindow.h
class TextEdit : public QTextEdit {
Q_OBJECT
...
public slots:
void receiveKeyPress(qulonglong);
...
};
mainwindow.cpp
void TextEdit::receiveKeyPress(qulonglong Number) {
if( Number == Qt::Key_Home )
...
}
recv-thread.h - created based on this link http://developer.qt.nokia.com/doc/qt-4.8/thread-basics.html#example-3-clock
#include <QThread>
#include "mainwindow.h" //To get TextEdit in here
class RecvThread : public QThread {
Q_OBJECT
signals:
void transferDataToSlot(qulonglong Data);
protected:
void run();
};
recv-thread.cpp
#include "recv-thread.h"
void RecvThread::run() {
unsigned long long int Number = NULL;
while(1) if(connected == 1) {
read(recvFileDescriptor, &Number, sizeof(unsigned long long int));
if( Number != NULL) {
emit transferDataToSlot(Number);
}
}
}
main.cpp
...
#include "recv-thread.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget window;
RecvThread recvThread;
...
QObject::connect(&recvThread, SIGNAL(transferDataToSlot(qulonglong)), window.edit1, SLOT(receiveKeyPress(qulonglong)), Qt::QueuedConnection); //line 38
recvThread.start();
//Displaying the window
window.show();
a.exec();
recvThread.quit();
recvThread.wait();
return 0;
}
Am I doing it right?

Display and get the result of a QMessageBox from outside of a QObject

I am trying to display and get the result a message box from outside of a QObject class. I seem to be able to generate the dialog like this:
#include <iostream>
#include <QApplication>
#include <QtConcurrentRun>
#include <QMessageBox>
class DialogHandler : public QObject
{
Q_OBJECT
signals:
void MySignal();
public:
DialogHandler()
{
connect( this, SIGNAL( MySignal() ), this, SLOT(MySlot()) );
}
void EmitSignal()
{
emit MySignal();
}
public slots:
void MySlot()
{
QMessageBox* dialog = new QMessageBox;
dialog->setText("Test Text");
dialog->exec();
int result = dialog->result();
if(result)
{
std::cout << "ok" << std::endl;
}
else
{
std::cout << "invalid" << std::endl;
}
}
};
#include "main.moc" // For CMake's automoc
void MyFunction(DialogHandler* dialogHandler)
{
dialogHandler->EmitSignal();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DialogHandler* dialogHandler = new DialogHandler;
MyFunction(dialogHandler);
return app.exec();
}
To get the result back in MyFunction, it seems to work to do simply pass an object to fill with the result like this:
#include <iostream>
#include <QApplication>
#include <QtConcurrentRun>
#include <QMessageBox>
class DialogHandler : public QObject
{
Q_OBJECT
signals:
void MySignal(int* returnValue);
public:
DialogHandler()
{
connect( this, SIGNAL( MySignal(int*) ), this, SLOT(MySlot(int*)), Qt::BlockingQueuedConnection );
}
void EmitSignal(int* returnValue)
{
emit MySignal(returnValue);
}
public slots:
void MySlot(int* returnValue)
{
std::cout << "input: " << *returnValue << std::endl;
QMessageBox* dialog = new QMessageBox;
dialog->addButton(QMessageBox::Yes);
dialog->addButton(QMessageBox::No);
dialog->setText("Test Text");
dialog->exec();
int result = dialog->result();
if(result == QMessageBox::Yes)
{
*returnValue = 1;
}
else
{
*returnValue = 0;
}
}
};
#include "main.moc" // For CMake's automoc
void MyFunction(DialogHandler* dialogHandler)
{
int returnValue = -1;
dialogHandler->EmitSignal(&returnValue);
std::cout << "returnValue: " << returnValue << std::endl;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DialogHandler* dialogHandler = new DialogHandler;
QtConcurrent::run(MyFunction, dialogHandler);
std::cout << "End" << std::endl;
return app.exec();
}
Does that seem reasonable? Is there a better way to do it?
This isn't possible quite like you have it, but with a bit of work it could be done. One option, of course, would be to convert your class to a QObject, at which point you could send signals. It doesn't help for the delay during exec, however. If that is necessary, you could have a messaging class that lives in the main UI thread, but can be called from other threads. The function called from other threads would need to lock, make a semaphore, and send an event to itself with the semaphore and message to be displayed. Then, in customEvent (which would be in the UI thread), you would create the message box, exec it, and trigger the semaphore after the message box is cleared.
Of course, things get a bit more complicated if you need to send information back the other way as well. Then you'll need a complete subsystem for your program, instead of just one basic class like I describe here.

how to send QList<Object *> objects to another class?

i'm trying to send a QList as a parameter to another class but for some reason i lose all it's content ...
(when i open the object with the debuger i see for objects...)
trying to send QList books to class Print:
class Store: public QWidget {
Q_OBJECT
public:
Analyze(QWidget *parent = 0);
void generate_report();
~Analyze();
private:
QList<Book *> books;
};
class Print
{
public:
Print();
bool generate_report_file(QList<Book *> *);
};
i'm sending books like this:
void Analyze::generate_report()
{
.
.
.
Print p;
if (!p.generate_report_file(&books))
QMessageBox::warning(this, "XML Escape","Error creating out.html", QMessageBox::Ok);
}
Small example
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QList>
#include <QString>
void print_list(QList<QString *> * k)
{
for (int i=0; i<k->size(); i++)
{
qDebug() << *k->at(i);
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<QString *> books;
books.append(new QString("asd"));
books.append(new QString("asdfgh"));
books.append(new QString("asdjhhhhhhtyut"));
print_list (&books);
return a.exec();
}
so just use * in function when calling elements of your QList, like in
qDebug() << *k->at(i); string
You should pass the QList by value. The reason, while this may seem silly on the surface, is that QList is implicitly shared. Please read http://doc.trolltech.com/latest/implicit-sharing.html to see more on that topic.
#include <QtCore/QtCore>
void
printList(const QStringList& list)
{
foreach (const QString& str, list) {
qDebug() << str;
}
}
int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
QStringList list;
list << "A" << "B" << "C";
printList(list);
return QCoreApplication::exec();
}
There is already a class called QStringList to use. Also, you would want to pass it by reference. Also, you do not want to use pointers on containers or QString. As they are automatically implicitly shared. So it's bad design to use pointers on those two.

Resources