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()
一样工作。我尝试过,但我做不到。我怎么能这样做?
答案 0 :(得分: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());
答案 1 :(得分:0)
我不确定你遇到什么问题,但一般来说,你想要创建一个继承QInputDialog
的类。添加一个静态方法,例如getPairedText()
,其签名与getText()
相同,但会返回QPair
。然后从函数中调用getText()
,传递所有相同的参数。当getText()
返回时,将字符串解析为QPair
并将其返回。
如果您在实施此问题时遇到任何具体问题,请在下面的评论中提问,我会尝试在答案中添加更多详细信息。