久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

QDialog exec() 并獲取結(jié)果值

QDialog exec() and getting result value(QDialog exec() 并獲取結(jié)果值)
本文介紹了QDialog exec() 并獲取結(jié)果值的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我已經(jīng)將 QDialog 子類化以實(shí)現(xiàn)類似于 QMessageBox 的功能(我需要它來允許自定義).它有一條短信和確定"、取消"按鈕.我正在使用 exec() 顯示對話框以使其阻塞.現(xiàn)在,當(dāng)用戶單擊確定/取消時(shí),我如何返回真/假值?

I have subclassed QDialog to implement functionality similar to QMessageBox ( I needed this to allow for customization). It has a text message and OK, Cancel buttons. I am showing the dialog using exec() to make it blocking. Now, how do I return values of true/false when the user clicks on OK/Cancel?

我嘗試將按鈕連接到 setResult() 然后,在單擊時(shí)返回結(jié)果值,但是

I tried connecting the buttons to setResult() and then, return the result value when clicked, but

  1. 單擊按鈕不會(huì)關(guān)閉對話框
  2. 返回值不正確.以下是我寫的代碼.我認(rèn)為我在執(zhí)行/結(jié)果部分錯(cuò)了 - 但我不知道如何解決它.

class MyMessageBox : public QDialog {
    Q_OBJECT

private slots:

    void onOKButtonClicked() { this->setResult(QDialog::Accepted); }
    void onCancelButtonClicked() { this->setResult(QDialog::Rejected); }

public:
    MyMessageBox(QMessageBox::Icon icon, const QString& title,
        const QString& text, bool showCancelButton = true,
        QWidget* parent = 0);

    virtual void resizeEvent(QResizeEvent* e);

    QDialog::DialogCode showYourself()
    {
        this->setWindowModality(Qt::ApplicationModal);
        this->exec();
        return static_cast<QDialog::DialogCode>(this->result());
    }
};

用戶將實(shí)例化該類并調(diào)用 showYourself(),它應(yīng)該返回值并關(guān)閉(和刪除)對話框.

The user will instantiate the class and call showYourself() which is expected to return the value and also close(and delete) the dialog.

我已經(jīng)發(fā)布了部分代碼.如果您需要更多,請告訴我,我會(huì)發(fā)布完整版本.

I have posted partial code. Let me know if you need more and I will post the complete version.

推薦答案

幾點(diǎn):

  1. 與其自己使用 setResult(),不如使用 QDialog::accept() 和 QDialog::r??eject().
  2. 看來您沒有充分利用信號(hào)和插槽.您需要?jiǎng)?chuàng)建對話(或另一個(gè))的對象來收聽對話的信號(hào).
  3. 在您的代碼中,您也沒有將信號(hào)連接到插槽.
  4. 在我的修復(fù)中 onOKButtonClickedonCancelButtonClicked 是不必要的.
  5. 通過我的修復(fù),您不需要 showYourself().只需調(diào)用 exec 和事件信息會(huì)流動(dòng).
  1. Rather than using setResult() yourself, use QDialog::accept() and QDialog::reject().
  2. It seems you are not taking full advantage of the signals and slots. You need the object which create the dialog (or another one) to listen to the signals of the dialog.
  3. In your code you are not connecting signals to slots either.
  4. With my fix onOKButtonClicked and onCancelButtonClicked are unnecessary.
  5. With my fix you don't need showYourself(). Just call exec and with the events information will flow.

您需要在顯示對話框之前添加此代碼(this 假設(shè)它在對話框方法中):

You need to add this code before showing the dialog (this assume it is in a dialog method):

QObject::connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
QObject::connect(rejectButton, SIGNAL(clicked()), this, SLOT(reject()));

在調(diào)用者對象中你有

void someInitFunctionOrConstructor(){
   QObject::connect(mydialog, SIGNAL(finished (int)), this, SLOT(dialogIsFinished(int)));
}

void dialogIsFinished(int){ //this is a slot
   if(result == QDialog::Accepted){
       //do something
       return
   }
   //do another thing
}

這篇關(guān)于QDialog exec() 并獲取結(jié)果值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 一区二区成人 | 国产精品一区一区 | 亚洲精选久久 | 色偷偷888欧美精品久久久 | 精品国产欧美 | 网黄在线| 在线视频三区 | 久久综合影院 | 欧一区 | 欧产日产国产精品视频 | 97超碰人人草 | 国产精品特级片 | 亚洲一区二区三区久久 | 久久成人免费观看 | 欧美一级黄色片免费观看 | 国产高清视频在线观看 | 日日噜噜噜夜夜爽爽狠狠视频97 | 国产视频一区二区 | 亚洲精品乱码久久久久久久久 | 欧美激情免费在线 | 亚洲国产一区在线 | 国产激情视频网 | 国产精品一码二码三码在线 | 精品在线观看入口 | 午夜激情一区 | 综合久久av | 欧洲精品视频一区 | 亚洲成人免费观看 | 色网站入口 | 国产高清在线视频 | 操操日 | 久久精品欧美一区二区三区不卡 | 国产一区二区在线免费观看 | 日本高清在线一区 | 在线免费观看欧美 | 特黄视频 | 国产999精品久久久影片官网 | 天天躁天天操 | 日韩三级一区 | 一本一道久久a久久精品综合 | 亚洲精品视频免费观看 |