16,211
社区成员
发帖
与我相关
我的任务
分享
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <qtimer.h>
QT_BEGIN_NAMESPACE
class QLabel;
class QPushbutton;
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
QTimer *timer;
QLabel *canTeenLabel;
QPushbutton *startButton;
QPushbutton *stopButton;
QVector<QString> vect();
int i;
//QString canteen;
public slots:
void start1();
void stop1();
void count1();
private:
};
#endif
main.cpp
#include <QApplication>
#include <QTextCodec>
#include "randomcanteen.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
MainWindow mainWin;
mainWin.show();
return app.exec();
}
randomCanteen.cpp
#include <QtGui>
#include "randomcanteen.h"
#include <qtimer.h>
#include <QtCore>
MainWindow::MainWindow()
{
QWidget *widget = new QWidget;
setCentralWidget(widget);
setWindowTitle(tr("随机选食堂"));
setMinimumSize(100, 200);
QVector<QString> vect(5);
vect[0]="东三";
vect[1]="东四";
vect[2]="集贸";
vect[3]="紫荆园";
vect[4]="教工";
i=0;
QLabel *canTeenLabel=new QLabel();
QPushButton *startButton=new QPushButton(tr("开始"));
QPushButton *stopButton=new QPushButton(tr("停止"));
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(count1()));
timer->start();
connect(startButton, SIGNAL(clicked()), this, SLOT(start1()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stop1()));
QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(startButton);
layout->addWidget(stopButton);
layout->addWidget(canTeenLabel);
widget->setLayout(layout);
}
void MainWindow::start1()
{
timer->start(1000);
}
void MainWindow::stop1()
{
timer->stop();
}
void MainWindow::count1()
{
i++;
if(i>4)
{
i=0;
}
canTeenLabel->setText(vect[i]);
/* 这一段也有问题,不注释掉的话运行不会出现界面,会出现 D:\Qt\randomCanteen-build-desktop\..\randomCanteen\randomCanteen.cpp:73: 错误:invalid types '<unresolved overloaded function type>[int]' for array subscript*/
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
class QLabel;
class QPushButton;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QTimer *timer;
QLabel *canTeenLabel;
QPushButton *startButton;
QPushButton *stopButton;
QVector<QString> vect;
int i;
public:
MainWindow(QWidget *parent = 0);
public slots:
void start1();
void stop1();
void count1();
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QTimer>
#include <QtGui>
#include <Qtimer.h>
#include <QtCore>
#include <QVector>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *widget = new QWidget;
setCentralWidget(widget);
setWindowTitle(tr("随机选食堂"));
setMinimumSize(100, 200);
canTeenLabel=new QLabel("Ready");
startButton=new QPushButton(tr("开始"));
stopButton=new QPushButton(tr("停止"));
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(count1()));
vect.push_back (tr("东三"));
vect.push_back(tr("东四"));
vect.push_back(tr("集贸"));
vect.push_back(tr("紫荆园"));
vect.push_back(tr("教工"));
connect(startButton, SIGNAL(clicked()), this, SLOT(start1()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stop1()));
connect (timer,SIGNAL(timeout()),this,SLOT(count1()));
QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(startButton);
layout->addWidget(stopButton);
layout->addWidget(canTeenLabel);
widget->setLayout(layout);
}
void MainWindow::start1()
{
timer->start(500);
}
void MainWindow::stop1()
{
timer->stop();
}
void MainWindow::count1()
{
i++;
if(i>4)
{
i=0;
}
canTeenLabel->setText(vect[i]);
}