2015-04-06 80 views
0

我想写一个交通灯GUI程序,但我得到的错误信息:无法添加到QImage的直接QGridLayout

C:\Qt\Qt5.3.0\Tools\QtCreator\bin\A2Q2\trafficlight.cpp:29: error: no matching function for call to '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(); 
} 

回答

2

你试图传递一个QImage*QWidget*QImage并非来自QWidget。如果参数需要QWidget*,则需要将其传递给QWidget*或指向从QWidget派生的类的实例。

你可以把图像转化为QLabel,如图this example

+0

非常感谢您!我会试试它!这是我第一次做Qt,所以有时我会感到困惑。 – mlh12