[Qt-interest] same code but different results on Windows and Mac

Yifei Li yifli at mtu.edu
Sun Oct 17 04:17:46 CEST 2010


Well, I was wrong.

Windows does not always show the two messages one after another. Actually, most of the time, those two messages appear together. 

However, following code does work. Can anyone explain why it works? Thanks

Yifei

#include <QtGui/QApplication>
#include <QWidget>
#include <QThread>
#include <QVBoxLayout>
#include <QTextEdit>

class Worker : public QThread {
	Q_OBJECT
signals:
	void displayMessage(QString);
protected:
	void run();	
};


#include "main.moc"

void Worker::run()
{
        emit displayMessage("hello1");
        sleep(1);
        emit displayMessage("hello2");
        sleep(1);
}


int main(int argc, char *argv[])
{
        QApplication a(argc, argv);
        QWidget* mainWidget = new QWidget;

        QVBoxLayout *layout = new QVBoxLayout;
        QTextEdit* view = new QTextEdit;
        layout->addWidget(view);
        mainWidget->setLayout(layout);

        mainWidget->show();
       
        Worker* w = new Worker;
        QObject::connect(w, SIGNAL(displayMessage(const QString&)), view, SLOT(append(const QString&)));
		 

        w->start();
        return a.exec();
}

On Oct 16, 2010, at 7:08 PM, Yifei Li wrote:

> Hi all,
> 
> In the following minimal example, I want to show two messages one after another with some delay in between. 
> Running this code on Windows 7 gives expected results, however, Mac OS (snow leopard) will show two messages together.
> 
> Can someone explain why there's such a difference? Thanks
> 
> Yifei
> 
> #include <QtGui/QApplication>
> #include <QWidget>
> #include <QThread>
> #include <QVBoxLayout>
> #include <QTextEdit>
> 
> class Plugin : public QObject {
>     Q_OBJECT
> public:
>     void exec();
> 
> signals:
>     void displayMessage(const QString&);
>     void pause();
> };
> 
> 
> class Worker : public QThread {
>         Q_OBJECT
> public:
>     Worker(Plugin* p) : m_plugin(p) {}
> protected:
>     void run() {m_plugin->exec();}
> private slots:
>     void pause() { sleep(1); }
> private:
>     Plugin* m_plugin;
> };
> 
> 
> #include "main.moc"
> 
> void Plugin::exec()
> {
>         emit displayMessage("hello1");
>         emit pause();
>         emit displayMessage("hello2");
>         emit pause();
> }
> 
> 
> int main(int argc, char *argv[])
> {
>         QApplication a(argc, argv);
>         QWidget* mainWidget = new QWidget;
> 
>         QVBoxLayout *layout = new QVBoxLayout;
>         QTextEdit* view = new QTextEdit;
>         layout->addWidget(view);
>         mainWidget->setLayout(layout);
> 
>         mainWidget->show();
>         Plugin* p = new Plugin;
>        Worker* w = new Worker(p);
>         QObject::connect(p, SIGNAL(pause()), w, SLOT(pause()));
>         QObject::connect(p, SIGNAL(displayMessage(const QString&)), view, SLOT(append(const QString&)));
> 		 
> 
>         w->start();
>         return a.exec();
> }
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101016/3a7f7a0e/attachment.html 


More information about the Qt-interest-old mailing list