2 GUI应用程序设计基础

46 阅读1分钟

image-20211218092246363

image-20211218102310495

ui是Ui::Widget,这和Widget不是一个

image-20211218102543054

image-20211218102718764

QMetaObject::connectSlotsByName(Widget);

这句话可以通过void Widget::on_checkBox_clicked(bool check){}这样的函数直接匹配,而不用手动添加槽

  • 如果字体出现乱码,可以使用QString::fromLocalBit8(“中文”);
  • 在字符串前面加上tr,tr(“中文”),tr代表翻译,QT会直接将字放到翻译文件中去
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>

Widget::Widget(QWidget *parent)
    :QWidget(parent)
    ,btn1(new QPushButton("确定",this))
    ,btn2(new QPushButton("取消",this))
    ,ckb1(new QCheckBox("粗体",this))
    ,ckb2(new QCheckBox("斜体",this))
    ,ckb3(new QCheckBox("下划线",this))
    ,txt(new QPlainTextEdit("ddddddd",this))
{
    QHBoxLayout*Hbox1 = new QHBoxLayout;
    Hbox1->addWidget(ckb1);
    Hbox1->addWidget(ckb2);
    Hbox1->addWidget(ckb3);
    QHBoxLayout*Hbox2 = new QHBoxLayout;
    Hbox2->addStretch();//加弹簧
    Hbox2->addWidget(btn1);
    Hbox2->addStretch();//加弹簧
    Hbox2->addWidget(btn2);
    QVBoxLayout* Vbox = new QVBoxLayout;
    Vbox->addLayout(Hbox1);
    Vbox->addWidget(txt);
    Vbox->addLayout(Hbox2);
    setLayout(Vbox);//将布局管理器交给窗体
}

image-20211218191721994

  • Checkable 表示按钮显示选中状态和非选中状态
setCentralWidget(ui->plainTextEdit);//设置主控件
void MainWindow::initUI()
{
    label = new QLabel("当前文件");
    ui->statusBar->addWidget(label);
    progressBar = new QProgressBar;
    progressBar->setMinimum(5);
    progressBar->setMaximum(50);
    progressBar->setValue(ui->plainTextEdit->font().pointSize());
    ui->statusBar->addWidget(progressBar);
    spinFont = new QSpinBox;
    ui->mainToolBar->addWidget(new QLabel("字体大小"));
    ui->mainToolBar->addWidget(spinFont);

    fontCom = new QFontComboBox;
    ui->mainToolBar->addWidget(new QLabel("字体"));
    ui->mainToolBar->addWidget(fontCom);
}

//设置文本格式
QTextCharFormat fmt;
ui.textEdit.mergeCurrentCharFormat(fmt);

image-20211218201945790

image-20211218203729193

主要要把图标放到工程文件中

image-20211218203852555

image-20211218204334665

image-20211218204624659