点击打开链接QColorDialog类的文档
QRgb:
#ifndef COLORDLG_H #define COLORDLG_H #include <QMainWindow> #include <QApplication> #include <QPushButton> #include <QDialog> #include <QGridLayout> #include <QWidget> #include <QHBoxLayout> #include <QLabel> #include <QLineEdit> #include <QComboBox> #include <QDialogButtonBox> #include <QGridLayout> #include <QDialog> #include <QtGui> #include <QString> #include <QVBoxLayout> #include <QInputDialog> #include <QMessageBox> #include <QColorDialog> namespace Ui { class ColorDlg; } class ColorDlg : public QDialog { Q_OBJECT public: explicit ColorDlg(QWidget *parent = 0); ~ColorDlg(); signals: void setTxt(QString txt); public slots: void inputtxt(); void show_clr(); private: Ui::ColorDlg *ui; QPushButton *btnok; QPushButton *btncolor; QLabel * labeltxt; QString txt; QWidget * pParent; }; #endif // COLORDLG_H
#include "colordlg.h" #include "ui_colordlg.h" ColorDlg::ColorDlg(QWidget *parent) : QDialog(parent), ui(new Ui::ColorDlg) { // ui->setupUi(this); 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(inputtxt())); connect(this,SIGNAL(setTxt(QString)), labeltxt,SLOT(setText(QString))); connect(btncolor,SIGNAL(clicked()),this,SLOT(show_clr())); resize(300,200); //改变窗口大小 setLayout(layout); } ColorDlg::~ColorDlg() { delete ui; } void ColorDlg::show_clr() { QColorDialog::setCustomColor(0,QRgb(0x0000FF)); QColor color = QColorDialog::getColor(QColor(0,255,0)); // QColor color = QColorDialog::getColor(QColor(0,255,0),pParent,tr("Color You Like")); 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 ColorDlg::inputtxt() { 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)); } }
#include <QApplication> #include "colordlg.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); ColorDlg w; w.show(); return a.exec(); }