Search code examples
c++qtqwidgetqframe

Windows becomes dim when button press takes longer time


How could I prevent the mainwindow goes to dim, even the button click event take long time to response.

For example: file myframe.h

class MyFrame : public QFrame
{
    Q_OBJECT
public:
explicit MyFrame(QWidget *parent=0);
~MyFrame();
void mousePressEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent *event);

private:
Ui::MyFrame *ui;
}

file myframe.cpp
MyFrame::MyFrame(QWidget *parent):
    QFrame(parent),ui(new Ui::FerryFrame)
{
    ui->setupUi(this);
}

void MyFrame::mouseReleaseEvent(QMouseEvent *event)
{
// do something cost long time,
// this will make the frame background becomes dim, 

}
MyFrame::~MyFrame()
{
}

Any solution?

Thanks, John


Solution

  • You need to start a thread for long running operations. You may need to manually gray/dim buttons or other options while your thread runs to prevent the user from issuing the same commands over and over. See the QT docs for specifics on threading:

    Threading in QT

    Pay special attention to thread pools. They are useful for GUI events. Also be aware of complexities of updating the user interface from threads.