Qt-interest Archive, May 2007
QThread Problem
Message 1 in thread
Hello,
I am trying to set a text for a QLabel in a QThread. If I try to run this simple programm, i get an error : QObject: Cannot create children for a parent that is in a different thread. I don´t know how to solve this problem.
class MyWindow : public QMainWindow, public QThread{
public:
MyWindow()
:infoWidget(new QWidget()),connections(new QLabel()) {
initLayout();
setCentralWidget(infoWidget);
QThread::start();
}
protected:
void run(void){
// The Thread is not able to set the Text for the QLabel
connections->setText("A Text");
}
void initLayout(void){
BorderLayout *layout = new BorderLayout;
layout->addWidget(connections,BorderLayout::East);
infoWidget->setLayout(layout);
}
private:
QWidget *infoWidget;
QLabel *connections;
};
Any suggestions ?
--
[ signature omitted ]
Message 2 in thread
å 2007-05-06æç 03:35 +0200ïThomas Slugaåéï
> Hello,
>
> I am trying to set a text for a QLabel in a QThread. If I try to run this simple programm, i get an error : QObject: Cannot create children for a parent that is in a different thread. I donÂt know how to solve this problem.
>
> class MyWindow : public QMainWindow, public QThread{
>
> public:
> MyWindow()
> :infoWidget(new QWidget()),connections(new QLabel()) {
> initLayout();
> setCentralWidget(infoWidget);
> QThread::start();
>
> }
> protected:
> void run(void){
> // The Thread is not able to set the Text for the QLabel
> connections->setText("A Text");
> }
> void initLayout(void){
> BorderLayout *layout = new BorderLayout;
> layout->addWidget(connections,BorderLayout::East);
> infoWidget->setLayout(layout);
> }
>
> private:
> QWidget *infoWidget;
> QLabel *connections;
> };
>
>
> Any suggestions ?
>
u'd better not make any GUI object inheritanced from QThread . I think u
want deal the text process in another thread, if that is true, u can
refer to Observer Pattern. it may be a good idea.
--
[ signature omitted ]
Message 3 in thread
On 06.05.07 03:35:31, Thomas Sluga wrote:
> Hello,
>
> I am trying to set a text for a QLabel in a QThread. If I try to run this simple programm, i get an error : QObject: Cannot create children for a parent that is in a different thread. I don´t know how to solve this problem.
>
> class MyWindow : public QMainWindow, public QThread{
Here's the first error, your class inherits from QObject twice, that
won't work. Second you're missing the Q_OBJECT macro which is mandatory
for any class that is a subclass of QObject (either direct or indirect).
> public:
> MyWindow()
> :infoWidget(new QWidget()),connections(new QLabel()) {
> initLayout();
> setCentralWidget(infoWidget);
> QThread::start();
> }
> protected:
> void run(void){
> // The Thread is not able to set the Text for the QLabel
> connections->setText("A Text");
And here you try to do GUI operations from a non-GUI thread which is
also forbidden (see the Qt documentation). What you probably really want
to do is have a QMainWindow with the label and start a QThread subclass
from there. Then the QThread subclass can either use Queued signals (in
case of Qt4) or custom events to talk to the main gui thread and tell it
what to display in the label.
Andreas
--
[ signature omitted ]