日期:2014-05-16  浏览次数:20633 次

QT开发平台
我在ubuntu下面安装了一个QT开发工具,但是后面进入QT Creator后,直接运行example都不行,我主要目的是
在源文件有的情况下,可以运行出一个出来,这是一个的猜数字游戏(网上下载的)
main.cpp

#include <QApplication>
#include "newfile.h"
//
int main(int argc, char ** argv)
{
QApplication app( argc, argv );
mywidget win;
win.show(); 

return app.exec();
}

newfile.cpp

#include <QtGui> 

 
#include "newfile.h" 
 
mywidget::mywidget(QWidget* parent):QWidget(parent) 

  label=new QLabel("time:");
  addButton=new QPushButton("Input"); 
  newButton=new QPushButton("New");
  edit=new QLineEdit(); 
  edittime=new QLineEdit("1");
  edittime->setReadOnly(true);
  val = rand() % 99 + 0;
  time=1;
  connect(  addButton,SIGNAL(clicked()),this,SLOT(estimate())); 
  connect(  newButton,SIGNAL(clicked()),this,SLOT(newgame())); 
  
    QHBoxLayout *HLayout = new QHBoxLayout;
    HLayout->addWidget(label);
    HLayout->addWidget(edittime);

    QVBoxLayout *VLayout = new QVBoxLayout;
    VLayout->addLayout(HLayout);
    VLayout->addWidget( edit);
    VLayout->addWidget(addButton);
    VLayout->addWidget(newButton);
    setLayout(VLayout); 
    setWindowTitle(QObject::trUtf8("猜数字了"));//设置标题 

void mywidget::estimate()
{   QString stemp;

    float ltemp;
    ltemp=edit->text().toFloat();
    stemp=QString::number(time);
    edittime->setText(stemp);
    time++;
    if(ltemp>val)
    {
      QMessageBox::about(this, tr("Message"),
            QObject::trUtf8("<h2>大了</h2>"));
               
         
    }
    if(ltemp<val)
    {
      QMessageBox::about(this, tr("Message"),
            QObject::trUtf8("<h2>小了</h2>"));
             
    }
     if(ltemp==val)
    {
      QMessageBox::about(this, tr("Message"),
           QObject::trUtf8("<h2>猜对了!!</h2>"));
         
    } 
    
}
void mywidget::newgame()
{   
QString stemp;
time=1;
stemp=QString::number(time);
    edittime->setText(stemp);
    edit->setText("");
   &nbs