可在以下网址查看资料
https://doc.qt.io/archives/qt-5.9/classes.html
效果展示:
可以点击上方的按钮对下面的文字进行修改。
mylayouttest.h
//mylayouttest.h
#ifndef MYLAYOUTTEST_H
#define MYLAYOUTTEST_H
#include <QMainWindow>
#include <QAbstractItemModel>
#include<QCheckBox>
#include<QRadioButton>
#include<QPlainTextEdit>
namespace Ui {
class MyLayoutTest;
}
class MyLayoutTest : public QWidget
{
Q_OBJECT
public:
explicit MyLayoutTest(QWidget *parent = 0);
~MyLayoutTest();
private slots:
void chkUnderLineSlot(bool checked);
void chkItalicLineSlot(bool checked);
void chkBoldLineSlot(bool checked);
void setTextFontColor();
private:
Ui::MyLayoutTest *ui;
QCheckBox ulCheckBox;
QCheckBox itCheckBox;
QCheckBox boldCheckBox;
QRadioButton redRadioButton;
QRadioButton blueRadioButton;
QRadioButton blackRadioButton;
QPlainTextEdit showTextEdit;
};
#endif // MYLAYOUTTEST_H
main.cpp
//main.cpp
#include "mylayouttest.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyLayoutTest w;
w.show();
return a.exec();
}
mylayouttest.cpp
//mylayouttest.cpp
#include "mylayouttest.h"
#include "ui_mylayouttest.h"
#include<QGroupBox>
#include<QHBoxLayout>
MyLayoutTest::MyLayoutTest(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyLayoutTest)
{
ui->setupUi(this);
QGroupBox *checkGroup=new QGroupBox(this);
QHBoxLayout *checkLayout=new QHBoxLayout;
this->ulCheckBox.setText("UnderLine");
this->itCheckBox.setText("Italic");
this->boldCheckBox.setText("Bold");
checkLayout->addWidget(&ulCheckBox);
checkLayout->addWidget(&boldCheckBox);
checkLayout->addWidget(&itCheckBox);
checkGroup->setLayout(checkLayout);
QGroupBox *radioGroup=new QGroupBox(this);
QHBoxLayout *radioLayout=new QHBoxLayout;
this->redRadioButton.setText("Red");
this->blueRadioButton.setText("Bule");
this->blackRadioButton.setText("Black");
radioLayout->addWidget(&redRadioButton);
radioLayout->addWidget(&blueRadioButton);
radioLayout->addWidget(&blackRadioButton);
radioGroup->setLayout(radioLayout);
QVBoxLayout *mainLayout= new QVBoxLayout;
mainLayout->addWidget(checkGroup);
mainLayout->addWidget(radioGroup);
mainLayout->addWidget(&showTextEdit);
this->setLayout(mainLayout);
// 信号与槽
connect(&this->ulCheckBox, SIGNAL(clicked(bool)), this, SLOT(chkUnderLineSlot(bool)));
connect(&boldCheckBox, SIGNAL(clicked(bool)), this, SLOT(chkBoldLineSlot(bool)));
connect(&itCheckBox, SIGNAL(clicked(bool)), this, SLOT(chkItalicLineSlot(bool)));
connect(&redRadioButton, SIGNAL(clicked(bool)), this, SLOT(setTextFontColor()));
connect(&blueRadioButton, SIGNAL(clicked(bool)), this, SLOT(setTextFontColor()));
connect(&blackRadioButton, SIGNAL(clicked(bool)), this, SLOT(setTextFontColor()));
}
MyLayoutTest::~MyLayoutTest()
{
delete ui;
}
void MyLayoutTest::chkUnderLineSlot(bool checked)
{
QFont font=showTextEdit.font();
font.setUnderline(checked);
showTextEdit.setFont(font);
}
void MyLayoutTest::chkItalicLineSlot(bool checked)
{
QFont font=showTextEdit.font();
font.setItalic(checked);
showTextEdit.setFont(font);
}
void MyLayoutTest::chkBoldLineSlot(bool checked)
{
QFont font=showTextEdit.font();
font.setBold(checked);
showTextEdit.setFont(font);
}
void MyLayoutTest::setTextFontColor()
{
QPalette plt = showTextEdit.palette();
if(redRadioButton.isChecked())
plt.setColor(QPalette::Text,Qt::red);
else if(blueRadioButton.isChecked())
plt.setColor (QPalette::Text,Qt::blue);
else if(blackRadioButton.isChecked())
plt.setColor(QPalette::Text,Qt::black);
showTextEdit.setPalette(plt);
}