首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 复习指导 >

C++应用实例六

2008-12-09 
QThread基础实例

    //threadTest.h

  #ifndef THREADTEST_H

  #define THREADTEST_H

  #include

  #include "test.h"

  class MyThread : public QThread

  {

  protected :

  virtual void run();

  };

  MyThread myThread;

  void Test::newSlot()

  {

  myThread.start();

  }

  #endif

  //threadTest.cpp

  #include

  void MyThread::run()

  {

  for(int i=0;i <100;i++)

  {

  printf("new value i is:%d",i);

  //QMessageBox::information(this,tr("info"),tr("load dll OK!"));

  //QMessageBox::information(this, tr("Empty Search "),"click Find.");

  //Test m;

  //m.show();

  qDebug("mhf");

  }

  /*

  QDialog *dialog = new QDialog(0,”popup”,FALSE);

  dialog->setCaption(“A QDialog Window”);

  dialog->setMinimumSize(200,80);

  dialog->setMaximumSize(200,80);

  dialog->show();

  */

  }

  //test.cpp

  #include "test.h"

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  #include

  /*

  * Constructs a Test as a child of ’parent’, with the

  * name ’name’ and widget flags set to ’f’.

  */

  Test::Test( QWidget* parent ): QWidget( parent=0 )

  {

  this->setWindowTitle("w");

  this->resize(250, 50);

  textEdit1 = new QTextEdit( "textEdit1" );

  textEdit1->setGeometry( QRect( 10, 60, 570, 291 ) );

  lineEdit1 = new QLineEdit( "lineEdit1" );

  lineEdit1->setGeometry( QRect( 10, 360, 571, 31 ) );

  pushButton2 = new QPushButton( "pushButton2" );

  pushButton2->setGeometry( QRect( 380, 400, 201, 41 ) );

  pushButton1 = new QPushButton( "pushButton1" );

  pushButton1->setGeometry( QRect( 10, 10, 261, 41 ) );

  //languageChange();

  resize( QSize(600, 480).expandedTo(minimumSizeHint()) );

  // signals and slots connections

  connect( pushButton1, SIGNAL( clicked() ), this, SLOT( newSlot() ) );

  connect( pushButton2, SIGNAL( clicked() ), this, SLOT( languageChange() ) );

  //add

  QVBoxLayout *layout=new QVBoxLayout;

  layout->addWidget(textEdit1);

  layout->addWidget(lineEdit1);

  layout->addWidget(pushButton1);

  layout->addWidget(pushButton2);

  this->setLayout(layout);

  this->resize(350, 200);

  }

  /*

  * Destroys the object and frees any allocated resources

  */

  Test::~Test()

  {

  // no need to delete child widgets, Qt does it all for us

  }

  /*

  * Sets the strings of the subwidgets using the current

  * language.

  */

  void Test::languageChange()

  {

  this->setWindowTitle("m");

  //printf("new value i is");

  pushButton2->setText( tr( "pushButton2" ) );

  pushButton1->setText( tr( "pushButton1" ) );

  }

  //test.h

  #ifndef TEST_H

  #define TEST_H

  #include

  #include

  class QVBoxLayout;

  class QHBoxLayout;

  class QGridLayout;

  class QLineEdit;

  class QPushButton;

  class QTextEdit;

  class Test : public QWidget

  {

  Q_OBJECT

  public:

  Test( QWidget* parent = 0 );

  ~Test();

  QTextEdit* textEdit1;

  QLineEdit* lineEdit1;

  QPushButton* pushButton2;

  QPushButton* pushButton1;

  public slots:

  virtual void newSlot();

  protected:

  protected slots:

  virtual void languageChange();

  };

  #endif // TEST_H

  main.cpp

  #include

  #include "test.h"

  int main( int argc, char ** argv )

  {

  QApplication a( argc, argv );

  Test w;

  w.show();

  a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );

  return a.exec();

  }

  /////////////

  调试运行

  gdb *.exe

  run step点击按钮就可以看到输出调试信息。

 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行