3
我需要實現一個搜索表單(我已經有這個函數),但我不知道如何追加(如果我可以這樣說)QlineEdit和QPushButton到一個QMessageBox。如何將QLineEdit包含在QMessageBox中作爲搜索表單
我需要實現一個搜索表單(我已經有這個函數),但我不知道如何追加(如果我可以這樣說)QlineEdit和QPushButton到一個QMessageBox。如何將QLineEdit包含在QMessageBox中作爲搜索表單
嘗試與它的佈局玩:
#include <QApplication>
#include <QMessageBox>
#include <QLineEdit>
#include <QPushButton>
#include <QLayout>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMessageBox mb;
QLineEdit le;
QPushButton bt;
mb.layout()->addWidget(&bt);
mb.layout()->addWidget(&le);
mb.show();
return app.exec();
}
你可以繼承了QDialog並把它作爲一個消息框
searchdialog.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
searchdialog.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();
}
main .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();
}
這真的很混亂。 – 2012-02-19 17:41:24
毫米,嘗試過,但QLineEdit就像一個按鈕,不會做任何事情。 – ddacot 2012-02-19 17:48:07
抱歉,我重新編輯它 – 2012-02-19 17:50:15