当前位置: 代码迷 >> QT开发 >> qtimer的有关问题
  详细解决方案

qtimer的有关问题

热度:18   发布时间:2016-04-25 04:49:45.0
qtimer的问题
刚学qt。 无聊想试着写一个 随机选食堂的程序。。。
用了qtimer,出现了问题。 搞了很久还是不知道问题在哪

求教各位。

头文件 randomcanteen.h

C/C++ code
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <qtimer.h>QT_BEGIN_NAMESPACEclass QLabel;class QPushbutton;QT_END_NAMESPACEclass MainWindow : public QMainWindow{    Q_OBJECTpublic:    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*/      }

`

------解决方案--------------------
帮你改了一下,自己对着看吧
mainwindow.h
C/C++ code
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QtGui/QMainWindow>class QLabel;class QPushButton;class MainWindow : public QMainWindow{    Q_OBJECTpublic:     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