最近在阅读Qt 5.9 C++开发指南,为了加深对书本上内容的理解,参照书上的讲解尝试写了一些demo,用于以后工作中查阅,如果涉及侵权请告知,实例程序samp2_3
mydialog.h
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
class MyDialog : public QDialog
{
Q_OBJECT
private:
QCheckBox *underlineCheckBox;
QCheckBox *italicCheckBox;
QCheckBox *boldCheckBox;
QRadioButton *blackRadioButton;
QRadioButton *redRadioButton;
QRadioButton *blueRadioButton;
QPlainTextEdit *plainTextEdit;
QPushButton *okPushButton;
QPushButton *cancelPushButton;
QPushButton *closePushButton;
public:
MyDialog(QWidget *parent = nullptr);
~MyDialog();
void iniUI();
void iniSignalSlots();
public slots:
void onUnderlineCheckBox(bool checked);
void onItalicCheckBox(bool checked);
void onBoldCheckBox(bool checked);
void setTextFontColor();
void onOkPushButton();
void onCancelPushButton();
};
#endif // MYDIALOG_H
mydialog.cpp
#include "mydialog.h"
MyDialog::MyDialog(QWidget *parent) : QDialog(parent)
{
iniUI();
iniSignalSlots();
}
MyDialog::~MyDialog()
{
}
/*
* 初始化界面
*/
void MyDialog::iniUI()
{
// 初始化三个复选框
underlineCheckBox = new QCheckBox(tr("Underline"));
italicCheckBox = new QCheckBox(tr("Italic"));
boldCheckBox = new QCheckBox(tr("Bold"));
// 将三个复选框放在一个水平布局内
QHBoxLayout \*hlayout1 = new QHBoxLayout();
hlayout1->addWidget(underlineCheckBox);
hlayout1->addWidget(italicCheckBox);
hlayout1->addWidget(boldCheckBox);
// 初始化三个单选按钮
blackRadioButton = new QRadioButton(tr("Black"));
blackRadioButton->setChecked(true);
redRadioButton = new QRadioButton(tr("Red"));
blueRadioButton = new QRadioButton(tr("Blue"));
// 将三个单选按钮放在一个水平布局内
QHBoxLayout \*hlayout2 = new QHBoxLayout();
hlayout2->addWidget(blackRadioButton);
hlayout2->addWidget(redRadioButton);
hlayout2->addWidget(blueRadioButton);
// 初始化文本框
plainTextEdit = new QPlainTextEdit();
// 初始化三个按钮
okPushButton = new QPushButton(tr("确定"));
cancelPushButton = new QPushButton(tr("取消"));
closePushButton = new QPushButton(tr("退出"));
// 将三个按钮放在一个水平布局内
QHBoxLayout \*hlayout3 = new QHBoxLayout();
hlayout3->addWidget(okPushButton);
hlayout3->addWidget(cancelPushButton);
hlayout3->addWidget(closePushButton);
// 将三个水平布局放在一个垂直布局内,并设置为窗口整体布局
QVBoxLayout \*vlayout = new QVBoxLayout();
vlayout->addLayout(hlayout1);
vlayout->addLayout(hlayout2);
vlayout->addWidget(plainTextEdit);
vlayout->addLayout(hlayout3);
setLayout(vlayout);
resize(,);
}
/*
* 初始化信号与槽
*/
void MyDialog::iniSignalSlots()
{
// 处理三个复选框
connect(underlineCheckBox, SIGNAL(clicked(bool)), this, SLOT(onUnderlineCheckBox(bool)));
connect(italicCheckBox, SIGNAL(clicked(bool)), this, SLOT(onItalicCheckBox(bool)));
connect(boldCheckBox, SIGNAL(clicked(bool)), this, SLOT(onBoldCheckBox(bool)));
// 处理三个单选按钮
connect(blackRadioButton, SIGNAL(clicked()), this, SLOT(setTextFontColor()));
connect(redRadioButton, SIGNAL(clicked()), this, SLOT(setTextFontColor()));
connect(blueRadioButton, SIGNAL(clicked()), this, SLOT(setTextFontColor()));
// 处理三个普通按钮
connect(okPushButton, SIGNAL(clicked()), this, SLOT(onOkPushButton()));
connect(cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButton()));
connect(closePushButton, SIGNAL(clicked()), this, SLOT(close()));
}
/*
* 当用户点击下划线复选框的槽函数
*/
void MyDialog::onUnderlineCheckBox(bool checked)
{
QFont font = plainTextEdit->font();
font.setUnderline(checked);
plainTextEdit->setFont(font);
}
/*
* 当用户点击斜体复选框的槽函数
*/
void MyDialog::onItalicCheckBox(bool checked)
{
QFont font = plainTextEdit->font();
font.setItalic(checked);
plainTextEdit->setFont(font);
}
/*
* 当用户点击加粗复选框的槽函数
*/
void MyDialog::onBoldCheckBox(bool checked)
{
QFont font = plainTextEdit->font();
font.setBold(checked);
plainTextEdit->setFont(font);
}
/*
* 当用户点击单选按钮的槽函数
*/
void MyDialog::setTextFontColor()
{
if(blackRadioButton->isChecked())
{
QPalette pa;
pa.setColor(QPalette::Text, Qt::black);
plainTextEdit->setPalette(pa);
}
else if(redRadioButton->isChecked())
{
QPalette pa;
pa.setColor(QPalette::Text, Qt::red);
plainTextEdit->setPalette(pa);
}
else
{
QPalette pa;
pa.setColor(QPalette::Text, Qt::blue);
plainTextEdit->setPalette(pa);
}
}
/*
* 当用户点击确定按钮的槽函数
*/
void MyDialog::onOkPushButton()
{
underlineCheckBox->setEnabled(false);
italicCheckBox->setEnabled(false);
boldCheckBox->setEnabled(false);
blackRadioButton->setEnabled(false);
redRadioButton->setEnabled(false);
blueRadioButton->setEnabled(false);
plainTextEdit->setEnabled(false);
okPushButton->setEnabled(false);
}
/*
* 当用户点击取消按钮的槽函数
*/
void MyDialog::onCancelPushButton()
{
underlineCheckBox->setEnabled(true);
italicCheckBox->setEnabled(true);
boldCheckBox->setEnabled(true);
blackRadioButton->setEnabled(true);
redRadioButton->setEnabled(true);
blueRadioButton->setEnabled(true);
plainTextEdit->setEnabled(true);
okPushButton->setEnabled(true);
underlineCheckBox->setChecked(false);
italicCheckBox->setChecked(false);
boldCheckBox->setChecked(false);
blackRadioButton->setChecked(true);
redRadioButton->setChecked(false);
blueRadioButton->setChecked(false);
onUnderlineCheckBox(false);
onItalicCheckBox(false);
onBoldCheckBox(false);
setTextFontColor();
}
main.cpp
#include "mydialog.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog w;
w.show();
return a.exec();
}
效果展示
手机扫一扫
移动阅读更方便
你可能感兴趣的文章