gpt4 book ai didi

c++ - 如何在搜索表单的 QMessageBox 中包含 QLineEdit

转载 作者:太空狗 更新时间:2023-10-29 21:27:18 25 4
gpt4 key购买 nike

我需要实现一个搜索表单(我已经有了这个功能),但我不知道如何将 QlineEdit 和 QPushButton 添加到 QMessageBox 中(如果我可以这样说的话)。

最佳答案

您可以继承 QDialog 并将其用作消息框

搜索对话框.h:

#ifndef SEARCHDIALOG_H
#define SEARCHDIALOG_H

#include <QDialog>
#include <QLineEdit>

class SearchDialog : public QDialog
{
Q_OBJECT
public:
explicit SearchDialog(QWidget *parent = 0);

QString searchString() const;

private:
QLineEdit *m_lineEdit;
};

#endif // SEARCHDIALOG_H

搜索对话框.cpp

#include "searchdialog.h"

#include <QVBoxLayout>
#include <QPushButton>
#include <QDialogButtonBox>

SearchDialog::SearchDialog(QWidget *parent) :
QDialog(parent)
{
m_lineEdit = new QLineEdit(this);

QPushButton *searchButton = new QPushButton(tr("Search"));
searchButton->setDefault(true);

QPushButton *cancelButton = new QPushButton(tr("Cancel"));

QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
buttonBox->addButton(searchButton, QDialogButtonBox::AcceptRole);
buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

QVBoxLayout *lt = new QVBoxLayout;
lt->addWidget(m_lineEdit);
lt->addWidget(buttonBox);

setLayout(lt);
}

QString SearchDialog::searchString() const
{
return m_lineEdit->text();
}

主要.cpp

#include <QtGui/QApplication>
#include <QtGui/QMessageBox>

#include <QLabel>

#include "searchdialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QLabel lbl;
SearchDialog *dialog = new SearchDialog(&lbl);
lbl.show();

QString searchString;

if (dialog->exec()) {
searchString = dialog->searchString();
lbl.setText(searchString);
}

return a.exec();
}

关于c++ - 如何在搜索表单的 QMessageBox 中包含 QLineEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9351407/

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