gpt4 book ai didi

c++ - 如何将静态方法添加到 QInputDialog 以返回自定义数据?

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:22 25 4
gpt4 key购买 nike

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]

此函数定义为调用对话框获取并返回将插入到 QLine 编辑中的文本。所以我想创建一个返回数据结构(比如 QPair)的 getData 静态方法,并像 QInputDialog::getText() 一样正常工作。我试过了,但我做不到。我该怎么做?

最佳答案

我已经做到了,但是我发帖是为了有需要的人。这是一个用于调整图像大小的对话框示例。更准确地说,它是为了向用户表示图像的当前大小,并为他/她提供一个界面来更改大小并使用 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());

关于c++ - 如何将静态方法添加到 QInputDialog 以返回自定义数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3726503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com