学习Qt5.9 C++开发指南

第二章
1.ui_XXX.h文件是对XXX.ui文件编译后生成的一个文件,即编译后会自动出现在编译后的目录下,所以想要出现在当前目录先,需要将项目构建设置中的Shadow build关闭,则文件会在当前目录下进行编译,从而ui_XXX.h文件也会出现在当前目录下,此时可以通过右击项目名称节点,选择"Add Existing Files…"找到文件即可。
2.代码化ui设计
所需组件需要在类的定义中确定,如下项目与其类如下所示
学习Qt5.9 C++开发指南_第1张图片
在类的定义中需要编写如下

private:
    QCheckBox *chkBoxUnder;
    QCheckBox *chkBoxItalic;
    QCheckBox *chkBoxBold;
    QRadioButton *rBtnBlack;
    QRadioButton *rBtnRed;
    QRadioButton *rBtnBlue;
    QPushButton *btOk;
    QPushButton *btCancel;
    QPushButton *btClose;
    QPlainTextEdit *txtEdit;
    void iniUI();        //初始化ui

在iniUI()函数中代码如下:

void QWDlgManual::iniUI()
{
    chkBoxBold = new QCheckBox(tr("Bold"));
    chkBoxUnder = new QCheckBox(tr("Underline"));
    chkBoxItalic = new QCheckBox(tr("Italic"));
    QHBoxLayout *HLay1 = new QHBoxLayout; 	 //水平布局
    HLay1->addWidget(chkBoxUnder);
    HLay1->addWidget(chkBoxItalic);
    HLay1->addWidget(chkBoxBold);
    rBtnBlack = new QRadioButton(tr("Black"));
    rBtnRed = new QRadioButton(tr("Red"));
    rBtnBlue = new QRadioButton(tr("Blue"));
    QHBoxLayout *HLay2 = new QHBoxLayout;
    HLay2->addWidget(rBtnBlack);
    HLay2->addWidget(rBtnRed);
    HLay2->addWidget(rBtnBlue);
    btOk = new QPushButton(tr("确认"));
    btCancel = new QPushButton(tr("取消"));
    btClose = new QPushButton(tr("退出"));
    QHBoxLayout *HLay3 = new QHBoxLayout;
    HLay3->addStretch();                 					//添加水平分隔的空格
    HLay3->addWidget(btOk);
    HLay3->addWidget(btCancel);
    HLay3->addStretch();
    HLay3->addWidget(btClose);
    txtEdit = new QPlainTextEdit;
    txtEdit->setPlainText(tr("Hello world\n\nIt is my demo"));
    QFont font = txtEdit->font();
    font.setPointSize(20);
    txtEdit->setFont(font);
    QVBoxLayout *VLay = new QVBoxLayout;	//垂直布局
    VLay->addLayout(HLay1);
    VLay->addLayout(HLay2);
    VLay->addWidget(txtEdit);
    VLay->addLayout(HLay3);
    setLayout(VLay);				//设置为窗体的主布局
}

3.QTextEdit组件
http://doc.qt.io/archives/qt-4.8/qtextedit.html 官方类说明
获取文本编辑框中选中的文本,通过类QTextCharFormat来实现。
如设置选中文本加下划线,代码如下:

	QTextCharFormat fmt;
	fmt = ui->txtEdit->currentCharFormat();
	fmt.setFontUnderline(checked);
	ui->txtEdit->mergeCurrentCharFormat(fmt);

再如设置选中文本加粗,代码如下:

    QTextCharFormat fmt;
    fmt = ui->txtEdit->currentCharFormat();
    if(checked)
        fmt.setFontWeight(QFont::Bold);
    else
        fmt.setFontWeight(QFont::Normal);
    ui->txtEdit->mergeCurrentCharFormat(fmt);

4.为应用程序设置图标
(1)将一个图标文件(必须是".ico"后缀的图标文件)复制到项目源程序目录下。
(2)在项目配置文件中(.pro)用RC_ICONS设置图标文件名,添加下面一行代码,

RC_ICONS = AppIcon.ico

你可能感兴趣的:(学习Qt5.9 C++开发指南)