dlg.h
#ifndef MYDLG_H #define MYDLG_H #include <QDialog> class QPushButton; class QLabel; class QString; class Mydlg:public QDialog { Q_OBJECT public: Mydlg(QWidget * parent = 0); signals: void setTxt(QString txt); public slots: void slot_input_txt(); void slot_show_clrdlg(); private: QPushButton *btnok; QPushButton *btncolor; QLabel * labeltxt; QString txt; QWidget * pParent; }; #endif // MYDLG_H
dlg.cpp
#include <QtGui> #include <QWidget> #include <QString> #include <QVBoxLayout> #include <QInputDialog> #include <QMessageBox> #include <QColorDialog> #include "mydlg.h" Mydlg::Mydlg(QWidget * parent ) : QDialog(parent) { btnok = new QPushButton("ok"); labeltxt = new QLabel("showtext"); btncolor = new QPushButton("color dlg"); QVBoxLayout *layout = new QVBoxLayout; pParent = parent; layout->addWidget(btnok); layout->addWidget(btncolor); layout->addWidget(labeltxt); connect(btnok, SIGNAL(clicked()), this, SLOT(slot_input_txt())); connect(this,SIGNAL(setTxt(QString)), labeltxt,SLOT(setText(QString))); connect(btncolor,SIGNAL(clicked()),this,SLOT(slot_show_clrdlg())); resize(300,200); setLayout(layout); } void Mydlg::slot_show_clrdlg() { QColorDialog::setCustomColor(0,QRgb(0x0000FF)); QColor color = QColorDialog::getColor(QColor(0,255,0)); QString str; if(color.isValid()){ str.sprintf("R:%d G:%d B:%d",color.red(), color.green(), color.blue()); QMessageBox::information(0,"Get Selected Color",str,QMessageBox::Ok,QMessageBox::Yes); } } void Mydlg::slot_input_txt() { bool isOk; txt = QInputDialog::getText(pParent, "Input Dialog", "Please input your Text", QLineEdit::Normal,"", &isOk); if(isOk) { QMessageBox ::information(pParent, "information", "Your comment is <b><color = red>" + txt + "</b></color>", QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes); emit(setTxt(txt)); } }
main.cpp
#include <QApplication> #include "mydlg.h" int main(int argc, char**argv) { QApplication app(argc, argv); Mydlg *dlg = new Mydlg; dlg->show(); return app.exec(); }
得到选择的RGB