I'm new to "C++ with Qt" programming, so I need help.
#include <iostream>
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QWidget>
#include <QFileDialog>
#include <QImage>
#include <QPixmap>
#include <QDir>
#include <QObject>
#include <QPictureFormatPlugin>
using namespace std;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
QGridLayout *layout = new QGridLayout(0);
QLabel *label = new QLabel("<H1><B><CENTER>Сканирование текста</CENTER> <B></H1>", 0);
QPushButton *button = new QPushButton("Открыть", 0);
label->setMargin(50);
QLabel *label2 = new QLabel("<H3><B><CENTER>Полученные данные</CENTER><B></H3>", 0);
label2->setMargin(50);
label2->setMinimumWidth(600);
QFileDialog dialog;
QStringList select;
dialog.setFileMode(QFileDialog::AnyFile);
QObject::connect(button, SIGNAL(clicked()), &dialog, SLOT(show()));
QLabel *lbl = new QLabel;
layout->addWidget(label, 0, 0,1,3,Qt::AlignCenter);
layout->addWidget(label2, 1, 2,Qt::AlignTop);
layout->addWidget(button,3, 1,Qt::AlignCenter);
layout->addWidget(lbl,2,1,0);
window->setLayout(layout);
window->show();
select = QFileDialog::getOpenFileNames(button, "Choose one or more files", "", "");
QPixmap pm(select.at(0));
lbl->setPixmap(pm);
return app.exec();
}
My program should do these things in order :
- Show the main window
- After I click a button (on the left), dialog appears.
- In the File Dialog I choose a picture and program gets the picture's directory.
- Finally, the picture above the button should update every time I press the button and choose another file in Dialog.
What I have:
The file dialog appears first before main window, and I don't know why. I choose a picture and the image in program updates immediately. But, after I press the button one more time and select a pic, nothing works.