-1

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 :

  1. Show the main window
  2. After I click a button (on the left), dialog appears.
  3. In the File Dialog I choose a picture and program gets the picture's directory.
  4. 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.

Wally Altman
  • 3,535
  • 3
  • 25
  • 33
newfag
  • 19
  • 4

1 Answers1

1

The file dialog appears first before main window, and I don't know why.

The reason why is because the window->show() command is not entirely synchronous -- that is, when you call window->show(), it posts some events into the event loop's event-queue, so that they can be acted on during the next iteration of the event loop. It is the processing of these events that cause the window to appear. However, the event loop executes inside app.exec(), which does not get called until after your call to QFileDialog::getOpenFileNames().

Since you do not want the file dialog to appear until after the user has clicked the button, you should not call QFileDialog::getOpenFileNames() in your main() function like that; instead, call it in a slot-method that is connected to your button's clicked() signal.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • QObject::connect(button,SIGNAL(clicked()), dialog, SLOT(getOpenFileName(button,"","","",""))); correct this please, I am total noob. I just need to connect this with button, as you mentioned. – newfag Jun 08 '15 at 05:04
  • You'll need to create your own subclass (of QLabel, or QMainWindow, or QWidget, or something, it's up to you, but let's say QLabel for now); then you'll need to add a "public slots:" section to your MyLabel subclass, and declare a MySlot() method in that section, and then put the code you want the button-click to invoke into the body of the MySlot() method. Then you can do a QOjbect::connect(button, SIGNAL(clicked), lbl, SLOT(MySlot()); to hook up the signal to MySlot(). (Be sure to create a MyLabel rather than a QLabel object too) – Jeremy Friesner Jun 08 '15 at 05:10
  • Thank you! It will definitely help! – newfag Jun 08 '15 at 05:15