0

In the project, I am trying to take an image from the user using a file directory, resize it with the value selected from the combobox, and show the resized version in the application, but only the size of the image is printed. How can I solve this problem? Since I'm new to this language and program, I don't know the basics very well, please help.

header code:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "qlabel.h"
#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void openImage();
    QLabel *label;

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;

};
#endif // MAINWINDOW_H

MainWindow code:

#include "mainwindow.h"
#include <QLabel>
#include "qdialog.h"
#include "qpushbutton.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QComboBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("Hello Label");
    resize(400,500);

    ui->comboBox->addItem("1", QVariant(1));
    ui->comboBox->addItem("2", QVariant(2));
    ui->comboBox->addItem("3", QVariant(3));
    ui->comboBox->addItem("4", QVariant(4));
    ui->comboBox->addItem("5", QVariant(5));
    ui->comboBox->addItem("6", QVariant(6));
    ui->comboBox->addItem("7", QVariant(7));
    ui->comboBox->addItem("8", QVariant(8));
    ui->comboBox->addItem("9", QVariant(9));
    ui->comboBox->addItem("10", QVariant(10));

    QVariant selectedData = ui->comboBox->itemData(ui->comboBox->currentIndex());
    int selectedValue = selectedData.toInt();

    label->setFixedSize(selectedValue*225,selectedValue*225);
    label->setScaledContents(true);

}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::openImage()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Resim Seç"), "", tr("Resim Dosyaları (*.png *.jpg *.bmp)"));
    if (!fileName.isEmpty()) {

        QPixmap pixmap(fileName);
        ui->label->setPixmap(pixmap.scaled(ui->label->size(), Qt::KeepAspectRatio));
        ui->label->setAlignment(Qt::AlignCenter);

    }
}

void MainWindow::on_pushButton_clicked()
{
    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::openImage);
}
1
  • 1
    You say that "only the size of the image is printed", but there's no trace of any "printing" in your code. Besides, if you added the QLabel to a layout (which is the correct approach), it's quite possible that the label has 0 size on startup, so pixmap.scaled() will return a null pixmap. You're also not implementing what happens when the user selects another item in the combo (which should be achieved by connecting to its currentIndexChanged signal). In any case, please provide the UI file (possibly removing any widget not relevant to the problem, while still leaving it reproducible). Commented Jul 7 at 0:18

0

Browse other questions tagged or ask your own question.