javascriptのpromptのようなDialogを作ってみた
[注意点]
- QtDesignerでダイアログのレイアウトが決まったら、トップレベルのレイアウトをGridレイアウトにして、サイズ調整を行うと良い
- vtableがどうのというエラーが発生したらqmake を実行しなおした方が良い
- on_オブジェクト名_シグナル名の関数宣言
- set_prompt_textをpublicにする
- コンストラクタの引数にはデフォルト値を設定しなければならない
- QStringは"文字列"でかまわない。
↓これができあがり。
あとは、okボタンを押したとき用のスロット関数を作成すべし(on_okButton_clicked()かな)
以下ソースのみ掲載
ui_prompt.h(mocによる自動生成)
/******************************************************************************** ** Form generated from reading ui file 'prompt.ui' ** ** Created: Sat Jan 5 00:44:38 2008 ** by: Qt User Interface Compiler version 4.3.3 ** ** WARNING! All changes made in this file will be lost when recompiling ui file! ********************************************************************************/ #ifndef UI_PROMPT_H #define UI_PROMPT_H #include <QtCore/QVariant> #include <QtGui/QAction> #include <QtGui/QApplication> #include <QtGui/QButtonGroup> #include <QtGui/QDialog> #include <QtGui/QGridLayout> #include <QtGui/QHBoxLayout> #include <QtGui/QLabel> #include <QtGui/QLineEdit> #include <QtGui/QPushButton> #include <QtGui/QSpacerItem> #include <QtGui/QVBoxLayout> class Ui_Prompt { public: QGridLayout *gridLayout; QVBoxLayout *vboxLayout; QLabel *label; QLineEdit *lineEdit; QHBoxLayout *hboxLayout; QSpacerItem *spacerItem; QPushButton *cancelButton; QPushButton *okButton; void setupUi(QDialog *Prompt) { if (Prompt->objectName().isEmpty()) Prompt->setObjectName(QString::fromUtf8("Prompt")); Prompt->resize(450, 116); gridLayout = new QGridLayout(Prompt); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); vboxLayout = new QVBoxLayout(); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); label = new QLabel(Prompt); label->setObjectName(QString::fromUtf8("label")); vboxLayout->addWidget(label); lineEdit = new QLineEdit(Prompt); lineEdit->setObjectName(QString::fromUtf8("lineEdit")); vboxLayout->addWidget(lineEdit); hboxLayout = new QHBoxLayout(); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); spacerItem = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); hboxLayout->addItem(spacerItem); cancelButton = new QPushButton(Prompt); cancelButton->setObjectName(QString::fromUtf8("cancelButton")); hboxLayout->addWidget(cancelButton); okButton = new QPushButton(Prompt); okButton->setObjectName(QString::fromUtf8("okButton")); okButton->setEnabled(false); hboxLayout->addWidget(okButton); vboxLayout->addLayout(hboxLayout); gridLayout->addLayout(vboxLayout, 0, 0, 1, 1); retranslateUi(Prompt); QMetaObject::connectSlotsByName(Prompt); } // setupUi void retranslateUi(QDialog *Prompt) { Prompt->setWindowTitle(QApplication::translate("Prompt", "Prompt", 0, QApplication::UnicodeUTF8)); label->setText(QApplication::translate("Prompt", "TextLabel", 0, QApplication::UnicodeUTF8)); cancelButton->setText(QApplication::translate("Prompt", "cancel", 0, QApplication::UnicodeUTF8)); okButton->setText(QApplication::translate("Prompt", "OK", 0, QApplication::UnicodeUTF8)); Q_UNUSED(Prompt); } // retranslateUi }; namespace Ui { class Prompt: public Ui_Prompt {}; } // namespace Ui #endif // UI_PROMPT_H
prompt.h
#ifndef PROMPT_H #define PROMPT_H #include <QDialog> #include "ui_prompt.h" class Prompt : public QDialog,private Ui::Prompt { Q_OBJECT public: Prompt(QWidget *parent=0,const QString &promptText="prompt",const QString &defaultText=("")); void set_prompt_text(const QString &); private slots: void on_lineEdit_textChanged(const QString &); }; #endif
prompt.cpp
#include <QDialog> #include "prompt.h" Prompt::Prompt(QWidget *parent,const QString &promptText,const QString &defaultText) : QDialog(parent) { setupUi(this); set_prompt_text(promptText); lineEdit->setText(defaultText); } void Prompt::on_lineEdit_textChanged(const QString &str) { okButton->setEnabled(!str.isEmpty()); } void Prompt::set_prompt_text(const QString &str) { QString prompt; prompt = str; prompt.append(": "); label->setText(prompt); }
main.cpp
#include <QApplication> #include "prompt.h" int main(int argc,char *argv[]) { QApplication app(argc,argv); Prompt *prompt = new Prompt(0,"Do you think about it?","nothing"); prompt->show(); return app.exec(); }