How to connect Qt combobox with pushbutton and some kind of widget that shows picture?

2.3k views Asked by At

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
1

There are 1 answers

3
Luca Angioloni On BEST ANSWER

Well your layout is ok but i suggest you use a QLabel to show the image.

So that's what I would do:

  1. On the controller class create an attribute to store the current combo box selection, maybe an integer or a string.
  2. connect to the signal changhed of the combobox a slot. (Easy way: right click on the combobox in the editor and select "Go to slot", then choose the currentIndexChanged(int index) or the string variation. See documentation)
  3. In the slot keep our variable updated so that each time you change the value on the combobox you change also the variable that will store its current state.
  4. Create a slot for the push button (same thing as the combobox. Use signal clicked()). In the slot you than insert te picture in the QLabel.
  5. To set the picture do:

    QPixmap image("/path/to/image/chosen/image.jpg"); //choose the path accordingly to the variable stored that mirrors the state of the combobox.
    ui->imageLabel->setPixmap(image); //change imageLabel to the name of your label.
    
  6. You are good to go.