简体   繁体   English

Qt progressBar获取Unhandled异常如何检测父窗口小部件何时完成加载

[英]Qt progressBar getting Unhandled exception how detect when parent widget is done loading

im using Qt QProgressBar and place it in the statusBar on my main window Like this in the constructor : 即时通讯使用Qt QProgressBar并将其放在我的主窗口的statusBar中这样在构造函数中:

pb = new QProgressBar(statusBar());
pb->setTextVisible(false);
pb->hide();
statusBar()->addPermanentWidget(pb);

then im running procsses (web page loadding in this case ) and trying to show the progress with : 然后我运行procsses(在这种情况下加载网页)并尝试显示进度:

connect(ui.webView, SIGNAL(loadProgress(int)), SLOT(setProgress (int)));
void myMainWindow:: setProgress(int progress)
{
pb->show();
pb->setRange(0,100);
pb->setValue(progress);
}

But im getting Unhandled exception when it comes to pb->show() I guess it has to do something with loading the parent main windows and then the progress bar I was reading about the QAbstractEventDispatcher and processEvents but not understood how to implement it . 但是当涉及到pb-> show()时我得到了未处理的异常我想它必须做一些事情,加载父主窗口,然后我正在阅读有关QAbstractEventDispatcher和processEvents的进度条,但不知道如何实现它。

i did small test and put the pb->show() function call in button click signal/slut that means im triggering the pb->show() after the web page and the mainwindows fully loaded and its working fine without the exception. 我做了一个小测试并将pb-> show()函数调用放在按钮点击信号/ slut中,这意味着我在网页和主窗口完全加载后触发pb-> show()并且其工作正常,没有例外。 that make me belive there is problem with the event processing. 让我相信事件处理存在问题。

here is the class : 这是班级:

class MainWindowMainWindowContainer : public QMainWindow 
{
    Q_OBJECT

public:
    MainWindowContainer(QWidget *parent = 0);

 public slots:

    void adjustLocation();
    void changeLocation();
    void adjustTitle();
    void setProgress(int p);
    void finishLoading(bool);
    void finishedSlot(QNetworkReply* reply);


private:
    Ui::OnLineBack_mainWindow ui;    
    int progress;

    void createWebViewActions();
    QProgressBar *pb;
    void setprogressBar(int progress,bool show);

 };

MainWindowContainer::MainWindowContainer(QWidget* parent) : 
    QMainWindow(parent),

{
 ui.setupUi(this);
 progress = 0;


createWebViewActions();
ui.webView->load(QUrl("www.cnnnn.com"));
ui.webView->show();

pb = new QProgressBar(statusBar());
pb->setTextVisible(false);
pb->hide();
statusBar()->addPermanentWidget(pb);
}
 void MainWindowContainer::createWebViewActions()
{
    connect(ui.webView, SIGNAL(loadFinished(bool)), SLOT(adjustLocation()));
    connect(ui.webView, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
    connect(ui.webView, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
    connect(ui.webView, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));
    connect(ui.webView, SIGNAL(linkClicked(const QUrl&)),this, SLOT(linkClicked(const QUrl&)));

}
void MainWindowContainer::setProgress(int p)
{
    progress = p;
    adjustTitle();
}
 void MainWindowContainer::adjustTitle()
{
    qApp->processEvents(); 

    pb->show();
    if (progress <= 0 || progress >= 100)
    {
        QString titl =  ui.webView->title();
        statusBar()->showMessage(titl);
        setprogressBar(-1,false);

    }
    else
    {

        statusBar()->showMessage(QString("%1 (%2%)").arg(ui.webView->title()).arg(progress));
        setprogressBar(progress,true);
    }
}

 void MainWindowContainer::finishLoading(bool)
{   
    progress = 100;
    adjustTitle();

}
 void MainWindowContainer::setprogressBar(int progress,bool show)
{
     if(show)
    {
        pb->show();
        pb->setRange(0,100);
        pb->setValue(progress);
    }
    else
    {
        pb->hide();
    } 
}

In your createWebViewActions() function you connect the signals to their respective slots. createWebViewActions()函数中,将信号连接到各自的插槽。 (One small remark, the connect for the titleChanged(QString) signal and adjustTitle() slot fails because they have different signatures) (一个小注释, titleChanged(QString)信号和adjustTitle()插槽的连接失败,因为它们有不同的签名)

Among others you are connecting the signal loadProgress(int) to slot setProgress(int) . 您将信号loadProgress(int)连接到插槽setProgress(int) In this slot you call adjustTitle() where the instruction pb->show() is being executed. 在此插槽中,您调用adjustTitle() ,其中正在执行指令pb-> show()。

Notice that you are calling the createWebViewActions() function before the call to QProgressBar constructor 请注意,在调用QProgressBar构造函数之前,您正在调用createWebViewActions()函数

(...)
createWebViewActions();  <------ here you establish the signal->slot connections
ui.webView->load(QUrl("www.cnnnn.com"));
ui.webView->show();

pb = new QProgressBar(statusBar()); <------ here you call the QProgressBar constructor
pb->setTextVisible(false);
pb->hide();
statusBar()->addPermanentWidget(pb);
(...)

I think that maybe this slot (setProgress()) is being called before the QProgressBar is constructed which triggers the Unhandled exception you are getting. 我想也许这个插槽(setProgress())在构造QProgressBar之前被调用,它触发了你得到的未处理异常。

You could try and move the call to the QProgressBar constructor so that it is created before the slots connection. 您可以尝试将调用移动到QProgressBar构造函数,以便在插槽连接之前创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM