16,238
社区成员
发帖
与我相关
我的任务
分享
#include <QApplication>
#include <QDialog>
#include <QTextStream>
#include <QThread>
class Wnds : public QObject
{
Q_OBJECT
public:
Wnds() : w(0) { }
public slots:
void showWnd(int x)
{
if (w != 0)
delete w;
switch(x)
{
case 1:
{
w = new QDialog();
w->setWindowTitle("1");
w->show();
break;
}
case 2:
{
w = new QDialog();
w->setWindowTitle("2");
w->show();
break;
}
}//end switch
}
private:
QDialog *w;
};
class CmdLine : public QThread
{
Q_OBJECT
protected:
void run()
{
while(1)
{
QTextStream in(stdin);
int x = -1;
in >> x;
emit DoGotCmdLine(x);
}
}
signals:
void DoGotCmdLine(int);
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Wnds w;
CmdLine c;
c.start();
QObject::connect(&c, SIGNAL(DoGotCmdLine(int)),
&w, SLOT(showWnd(int)), Qt::QueuedConnection);
return a.exec();
}
#include "main.moc" //这里是实现文件写到cpp里面才需要的,在qtcreator里面先qmake,再编译方可通过.
原型,没有多做处理.比如线程就没有处理退出.