我正在尝试编写一个Trafficlight GUI程序,但是收到错误消息:
C:\ Qt \ Qt5.3.0 \ Tools \ QtCreator \ bin \ A2Q2 \ trafficlight.cpp:29:错误:没有匹配函数来调用'QGridLayout :: addWidget(QImage *&,int,int)' layout-> addWidget(image,2,1);
我甚至无法显示一张图片,必须有三张。
trafficlight.h
#ifndef TRAFFICLIGHT_H
#define TRAFFICLIGHT_H
#include "trafficpic.h"
#include <QWidget>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
class TrafficLight: public QWidget{
Q_OBJECT
public:
//constructor
TrafficLight();
private slots:
//set timer
void setTimer();
//display image
void displayImage();
//cancel timer
void cancelTimer();
private:
//widget data members
QLabel* timerLabel;
QLineEdit* timerEdit;
QPushButton* okButton;
QPushButton* cancelButton;
QImage* image;
//timer
QTimer* timer;
//instance of image
TrafficPic tf;
//sets up the GUI and connects signals and slots
void setUpGUI();
};
#endif // TRAFFICLIGHT_H
trafficlight.cpp
#include "trafficlight.h"
#include <QGridLayout>
#include <QMessageBox>
#include <stdlib.h>
#include <time.h>
//constructor
TrafficLight::TrafficLight(){
//initializes the timer
timer = new QTimer(this);
//sets up the GUI
setUpGUI();
}
//sets up the GUI and connects signals and slots
void TrafficLight::setUpGUI(){
//initializes all the widgets
timerLabel = new QLabel("Set Timer [Range: 2000 - 10000 ms] ");
timerEdit = new QLineEdit();
okButton = new QPushButton("Set Timer");
image = new QImage();
cancelButton = new QPushButton("Cancel");
//creates a layout and places widgets on the layout
QGridLayout* layout = new QGridLayout();
layout->addWidget(timerLabel, 0, 0);
layout->addWidget(timerEdit, 0, 1);
layout->addWidget(okButton, 1, 1);
layout->addWidget(image, 2, 1);
layout->addWidget(cancelButton, 3, 1);
//layout is set to the root widget
this->setLayout(layout);
//title of the root widget is set
this->setWindowTitle("Traffic Light");
//signals and slots are connected
connect(okButton,SIGNAL(clicked()), this, SLOT(setTimer()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancelTimer()));
connect(timer, SIGNAL(timeout()),this, SLOT(displayImage()));
}
//checks if the user input is valid and starts the timer
void TrafficLight::setTimer(){
bool ok;
QString timerText = timerEdit->text();
int timerValue = timerText.toInt(&ok, 10);
if (ok && (timerValue >= 2000) && (timerValue <= 10000)){
timer->setInterval(timerValue);
timer->start();
timerEdit->setReadOnly(true);
okButton->setDown(true);
}
else{
QMessageBox:: information(this, "Invalid timer value", "Invalid or out of range timer value.");
}
}
//display the image
void TrafficLight::displayImage(){
tf.getImage();
}
//stops the timer and allows user to reset the timer
void TrafficLight::cancelTimer(){
timer->stop();
okButton->setDown(false);
timerEdit->clear();
timerEdit->setReadOnly(false);
}
trafficpic.h
#ifndef TRAFFICPIC_H
#define TRAFFICPIC_H
#include <QImage>
class TrafficPic{
public:
//no-arg constructor
TrafficPic();
//returns an image
QImage getImage();
private:
//to store image
QImage image;
};
#endif // TRAFFICPIC_H
trafficpic.cpp
#include "trafficpic.h"
//#include "cstdlib"
//#include "ctime"
#include <QLabel>
//constructor initializing data member with image
TrafficPic::TrafficPic(){
image.load("C:/Qt/images/Green.jpg");
QLabel myLabel;
myLabel.setPixmap(QPixmap::fromImage(image));
myLabel.show();
}
//returns an image
QImage TrafficPic::getImage(){
return image;
}
的main.cpp
#include <QApplication>
#include "trafficlight.h"
int main(int argc, char *argv[]){
QApplication a(argc, argv);
TrafficLight tl;
tl.show();
return a.exec();
}
答案 0 :(得分:2)
您正尝试将QImage*
作为QWidget*
传递。 QImage
并非来自QWidget
。如果参数采用QWidget*
,则需要传递QWidget*
或指向从QWidget
派生的类的实例化的指针。
您可以将图像放入QLabel,如this example
所示