2010-09-16 19 views
1
QString QInputDialog::getText (QWidget * parent, const QString & title, 
    const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal, 
    const QString & text = QString(), bool * ok = 0, 
    Qt::WindowFlags flags = 0) [static] 

該函數被定義爲調用對話框get和返回將被插入到QLine編輯中的文本。所以我想創建一個返回數據結構(比如說QPair)的getData靜態方法,並且像QInputDialog::getText()一樣工作。我嘗試過,但我無法做到這一點。我怎樣才能做到這一點?如何將靜態方法添加到QInputDialog以返回自定義數據?

+0

你要買修改QInputDialog類本身? – jrharshath 2010-09-16 12:18:42

+0

你能更具體地說明你正在嘗試做什麼嗎?你想輸入兩個你想存儲在QPair中的字符串嗎? – 2010-09-16 12:25:14

+0

是的,我想輸入兩個字符串,我想存儲在QPair中。 – Narek 2010-09-16 12:35:50

回答

1

我已經做到了,但是我發佈了需要此功能的人。這是一個對話框的示例,用於調整圖像的大小。更準確地說,它代表用戶當前的圖像大小,併爲他/她提供一個界面來改變尺寸並用QPair獲得新的尺寸。

class ResizeImageDialog : public QDialog 
{ 
    Q_OBJECT 

double     m_ratio; 

QLabel     *m_widthLabel; 
QLabel     *m_hightLabel; 
QDoubleSpinBox   *m_widthSpinBox; 
QDoubleSpinBox   *m_hightSpinBox; 
QCheckBox    *m_keepRatioCheckBox; 

QPushButton    *m_okButton; 
QPushButton    *m_cancelButton; 

QHBoxLayout    *m_widthLayout; 
QHBoxLayout    *m_hightLayout; 
QHBoxLayout    *m_buttonLayout; 
QVBoxLayout    *m_generalLayout; 

private slots: 
    void widthChanged(double width); 
    void hightChanged(double hight); 

public: 
    ResizeImageDialog(QWidget * parent = 0, double imageWidth = 100.0, double imageHight = 100.0):QDialog(parent) 
    { 
    m_widthLabel = new QLabel("Image width"); 
    m_hightLabel = new QLabel("Image hight"); 

    m_widthSpinBox = new QDoubleSpinBox; 
    m_widthSpinBox->setMaximum(1500); 
    m_widthSpinBox->setValue(imageWidth); 
    connect(m_widthSpinBox, SIGNAL(valueChanged(double)), this, SLOT(widthChanged(double))); 


    m_hightSpinBox = new QDoubleSpinBox; 
    m_hightSpinBox->setMaximum(1500); 
    m_hightSpinBox->setValue(imageHight); 
    connect(m_hightSpinBox, SIGNAL(valueChanged(double)), this, SLOT(hightChanged(double))); 

    m_ratio = imageWidth/imageHight; 


    m_keepRatioCheckBox = new QCheckBox("Keep ratio",this); 
    m_keepRatioCheckBox->setChecked(true); 


    m_widthLayout = new QHBoxLayout; 
    m_widthLayout->addWidget(m_widthLabel); 
    m_widthLayout->addWidget(m_widthSpinBox); 

    m_hightLayout = new QHBoxLayout; 
    m_hightLayout->addWidget(m_hightLabel); 
    m_hightLayout->addWidget(m_hightSpinBox); 

    m_okButton = new QPushButton("OK"); 
    connect(m_okButton, SIGNAL(clicked()), this, SLOT(accept())); 

    m_cancelButton = new QPushButton("Cancel"); 
    connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(reject())); 

    m_buttonLayout = new QHBoxLayout; 
    m_buttonLayout->addStretch(); 
    m_buttonLayout->addWidget(m_okButton); 
    m_buttonLayout->addWidget(m_cancelButton); 

    m_generalLayout = new QVBoxLayout; 
    m_generalLayout->addLayout(m_widthLayout); 
    m_generalLayout->addLayout(m_hightLayout); 
    m_generalLayout->addWidget(m_keepRatioCheckBox); 
    m_generalLayout->addLayout(m_buttonLayout); 
    setLayout(m_generalLayout); 

    setMaximumSize(300, 300); 
    setModal(true); 
    //resize(670,630); 
    setWindowTitle(tr("Resize Image")); 
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); 
} 

static QPair<double, double> getNewSize(QWidget * parent = 0, double imageWidth = 100.0, double imageHight = 100.0) 
{ 
    ResizeImageDialog dlg(parent, imageWidth, imageHight); 
    dlg.exec(); 



    QPair<double, double> size; 
    size.first = dlg.m_widthSpinBox->value(); 
    size.second = dlg.m_hightSpinBox->value(); 
    return size; 
} 
}; 

現在你可以這樣做:

QPair<double, double> size = ResizeImageDialog::getNewSize(this, newImageFormat.width(), newImageFormat.height()); 
+0

爲什麼不使用QSizeF作爲getNewSize()的返回類型?它會比通用的QPair 更具信息性,並且會減少輸入。 – 2010-09-18 21:26:59

+0

這只是一個例子,下次使用時您可能會返回您創建的數據結構。 – Narek 2010-09-21 07:39:45

0

我不確定你有什麼問題,但一般情況下,你想創建一個繼承QInputDialog的類。添加一個靜態方法,如getPairedText(),其簽名與getText()相同,但返回QPair。然後從你的函數調用getText(),傳遞所有相同的參數。當getText()返回時,將該字符串解析爲QPair並將其返回。

如果您在實施時遇到任何具體問題,請在下面的評論中提問,我會嘗試在答案中添加更多詳細信息。

相關問題