UI+代码混合开发
UI控件
代码
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProgressBar>
#include <QLabel>
#include <QSpinBox>
#include <QFontComboBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QLabel *fLabCurFile;
QProgressBar *progressBar;
QSpinBox *spinFontSize;
QFontComboBox *comboFont;
void initUI();
void initSingalSlots(); //需要将自定义的槽和信号绑定在一起
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_actFontBold_triggered(bool checked);
void on_actFontItalic_triggered(bool checked);
void on_actFontUnder_triggered(bool checked);
void on_textEdit_copyAvailable(bool b);
void on_textEdit_selectionChanged();
/* 如果是自己写的控件,就要自己命名槽函数 */
void on_spinBoxFontSize_valueChanged(int aFontSize); //改变字体大小
void on_comboFont_currentIndexChanged(const QString &arg1); //改变字体,const 表示改变不了原有库的数据
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::initUI()
{
/* 设置QLabel */
fLabCurFile = new QLabel();
fLabCurFile->setMinimumWidth(150); //设置最小宽度
fLabCurFile->setText("当前文件:");
ui->statusbar->addWidget(fLabCurFile);
/* 设置QProgressBar */
progressBar = new QProgressBar;
progressBar->setMinimum(5);
progressBar->setMaximum(50);
progressBar->setValue(ui->textEdit->font().pointSize());
ui->statusbar->addWidget(progressBar);
/* 设置QSpinBox */
spinFontSize = new QSpinBox;
spinFontSize->setMinimum(5);
spinFontSize->setMaximum(50);
ui->mainToolBar->addWidget(new QLabel("字体大小"));
ui->mainToolBar->addWidget(spinFontSize);
/* 设置QComboBox */
comboFont = new QFontComboBox;
ui->mainToolBar->addWidget(new QLabel("字体"));
ui->mainToolBar->addWidget(comboFont);
setCentralWidget(ui->textEdit); //将中间的文本框中心布局
}
void MainWindow::initSingalSlots()
{
//检测到值发生变化的时候
connect(spinFontSize, SIGNAL(valueChanged(int)),
this, SLOT(on_spinBoxFontSize_valueChanged(int)));
connect(comboFont, SIGNAL(currentIndexChanged(const QString&)),
this, SLOT(on_comboFont_currentIndexChanged(const QString&)));
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//写好了调用
initUI();
initSingalSlots();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actFontBold_triggered(bool checked)
{
QTextCharFormat fmt;
if(checked)
fmt.setFontWeight(QFont::Bold);
else
fmt.setFontWeight(QFont::Normal);
ui->textEdit->mergeCurrentCharFormat(fmt);
}
void MainWindow::on_actFontItalic_triggered(bool checked)
{
QTextCharFormat fmt;
fmt.setFontItalic(checked);
ui->textEdit->mergeCurrentCharFormat(fmt);
}
void MainWindow::on_actFontUnder_triggered(bool checked)
{
QTextCharFormat fmt;
fmt.setFontUnderline(checked);
ui->textEdit->mergeCurrentCharFormat(fmt);
}
void MainWindow::on_textEdit_copyAvailable(bool b)
{
ui->actCut->setEnabled(b); //允许拷贝
ui->actCopy->setEnabled(b);
ui->actPaste->setEnabled(ui->textEdit->canPaste());
}
void MainWindow::on_textEdit_selectionChanged()
{
/* 选中了一段文件,可以看到按钮是不是被选中 */
QTextCharFormat fmt;
fmt = ui->textEdit->currentCharFormat(); //获取当前状态
ui->actFontItalic->setChecked(fmt.fontItalic());
ui->actFontUnder->setChecked(fmt.fontUnderline());
ui->actFontBold->setChecked(fmt.font().bold());
}
void MainWindow::on_spinBoxFontSize_valueChanged(int aFontSize)
{
QTextCharFormat fmt;
fmt.setFontPointSize(aFontSize);
ui->textEdit->mergeCurrentCharFormat(fmt);
progressBar->setValue(aFontSize); //下面的百分比会跟着改变
}
void MainWindow::on_comboFont_currentIndexChanged(const QString &arg1)
{
QTextCharFormat fmt;
fmt.setFontFamily(arg1);
ui->textEdit->mergeCurrentCharFormat(fmt);
}
效果
主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈