I've just started to learn Qt and I want to make a simple program where I could select a picture name (in combobox) then click the pushbutton and selected picture would appear in widget(?) (in the same window if it's possible). It should look like this:
The biggest problem that I've faced so far is connecting all those objects together, I can't make them work properly.
Also I've tried to upload picture to widget but it appears only in full size and my program becomes a picture and nothing else.
EDIT:
I am trying to make it work, but I can't achieve that.. That's my code:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
connect(ui->comboBox, SIGNAL(currentIndexChanged(int index)), this, SLOT(on_pushButton_clicked(int index)));
}
void MainWindow::choiceChanged(int index)
{
switch (index) {
case 0:
firstPicture();
break;
case 1:
secondPicture();
break;
case 2:
thirdPicture();
break;
}
}
void MainWindow::on_pushButton_clicked(int index)
{
connect(ui->pushButton, SIGNAL(on_pushButton_clicked(int)), this, SLOT(choiceChanged(int)));
}
void MainWindow::firstPicture(){
QPixmap image("C:/Documents/Aaaa.png");
ui->label->setPixmap(image);
}
void MainWindow::secondPicture(){
QPixmap image("C:/Documents/Bbbb.png");
ui->label->setPixmap(image);
}
void MainWindow::thirdPicture(){
QPixmap image("C:/Documents/Cccc.png");
ui->label->setPixmap(image);
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked(int index);
void choiceChanged(int index);
void on_comboBox_currentIndexChanged(int index);
void firstPicture();
void secondPicture();
void thirdPicture();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Well your layout is ok but i suggest you use a QLabel to show the image.
So that's what I would do:
currentIndexChanged(int index)
or the string variation. See documentation)clicked()
). In the slot you than insert te picture in the QLabel.To set the picture do:
You are good to go.