qtimer的问题

avrilers 2012-04-15 09:17:20
刚学qt。 无聊想试着写一个 随机选食堂的程序。。。
用了qtimer,出现了问题。 搞了很久还是不知道问题在哪

求教各位。

头文件 randomcanteen.h

#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*/

}

`
...全文
431 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
avrilers 2012-04-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

帮你改了一下,自己对着看吧
mainwindow.h
C/C++ code

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
class QLabel;
class QPushButton;

class MainWindow : public QMainWindow
{
Q_OBJEC……
[/Quote]

谢谢啊 因为是初学,什么都不懂。
rophie 2012-04-22
  • 打赏
  • 举报
回复
帮你改了一下,自己对着看吧
mainwindow.h

#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



mainwindow.cpp

#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]);
}



顺便说一句,编程要细心一点,发现你很多都是大小写错误,还有就是你QLabel,QPushButton等等这些在头文件声明了但是在源文件中却又重新声明和定义。对照看看你自己具体错在哪里吧,都是基础部分。
avrilers 2012-04-21
  • 打赏
  • 举报
回复
这个是忘了删了。 删了也不行啊

[Quote=引用 4 楼 的回复:]

你不是点击button后才开始的吗
void MainWindow::start1()
{
timer->start(1000);

}

你在上面写个timer->start() 明显是错误的啊
[/Quote]
  • 打赏
  • 举报
回复
注释的那段也待遇到过 原因没细想 QString strTemper = vect[i]; canTeenLabel->setText( strTemper );这样应该没问题

[Quote=引用 3 楼 的回复:]

引用 1 楼 的回复:

QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(count1()));

timer->start();//这个是干什么的?

connect(startButton, SIGNAL(clicked()), this, SLOT(start1()));……
[/Quote]
念茜 2012-04-20
  • 打赏
  • 举报
回复
你不是点击button后才开始的吗
void MainWindow::start1()
{
timer->start(1000);

}

你在上面写个timer->start() 明显是错误的啊
avrilers 2012-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(count1()));

timer->start();//这个是干什么的?

connect(startButton, SIGNAL(clicked()), this, SLOT(start1()));
connec……
[/Quote]

楼下回答对了。 求教为什么啊
念茜 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(count1()));

timer->start();//这个是干什么的?

connect(startButton, SIGNAL(clicked()), this, SLOT(start1()));
connec……
[/Quote]

一定是刚开始测试,后来忘记删掉了哈
  • 打赏
  • 举报
回复
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()));

16,211

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧