Reputation: 21
I am trying to write a trafficlight GUI program but I get the error message:
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);
I can't even get one image to show and there must be three.
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();
}
Upvotes: 2
Views: 4424
Reputation: 14685
You are trying to pass a QImage*
as a QWidget*
. QImage
does not derive from QWidget
. If a parameter takes a QWidget*
, you need to pass it either a QWidget*
, or a pointer to an instantiation of a class that is derived from QWidget
.
You can put the image into a QLabel, as shown in this example
Upvotes: 3