[Qt练习]QColorDialog

31 篇文章 5 订阅


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();
}

color
点击color dlg

color1

得到选择的RGB

color3


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ruglcc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值