Previous post:
QT implements serial debugging assistant (6): page layout
General serial port debugging tools have a function panel to modify the font of the serial port receiving box, let's add one:
1. Create a font setting panel first
For example, if we create a widget and name it letterFormWindow class, QTcreator will automatically generate .cpp, .h and .ui files
2. Add some components to the UI interface, as shown in the figure:
3. Now go back to the main interface file writing, and add a shortcut button in the main interface command bar
QMenuBar *menuBar = ui->menuBar;
QAction *letterPanlAct = menuBar->addAction("字体设置");
4. Bind this shortcut button to the creation of the letterFormWindow window just now, so click the shortcut key to call up the font setting panel
connect(letterPanlAct,&QAction::triggered,
[=] ()
{
if(letterFormUi == NULL)
{
letterFormUi = new letterFormWindow;
connect(letterFormUi, SIGNAL(sendFont(QFont)), this, SLOT(receiveFont(QFont)));
}
letterFormUi->show();
}
);
5. Pay attention to this sentence connect(letterFormUi, SIGNAL(sendFont(QFont)), this, SLOT(receiveFont(QFont))), bind the sendFont function event of the sub-interface to the receiveFont function of the main interface, which is used to connect the sub-interface The set font is transferred to the main interface for modification.
6. Sub-interface realization: header file
class letterFormWindow : public QWidget
{
Q_OBJECT
public:
explicit letterFormWindow(QWidget *parent = 0);
~letterFormWindow();
private slots:
void on_buttonBox_accepted();
void on_fontComboBox_currentFontChanged(const QFont &f);
void on_spinBox_valueChanged(int arg1);
void ChangeFont(void);
signals:
void sendFont(QFont); //用来传递数据的信号
private:
Ui::letterFormWindow *letterUi;
QFont tempFont; //缓存字体
};
CPP file:
#include "letterformwindow.h"
#include "ui_letterformwindow.h"
letterFormWindow::letterFormWindow(QWidget *parent) :
QWidget(parent),
letterUi(new Ui::letterFormWindow)
{
letterUi->setupUi(this);
letterUi->spinBox->setValue(10);
}
letterFormWindow::~letterFormWindow()
{
delete letterUi;
}
void letterFormWindow::on_buttonBox_accepted()
{
emit sendFont(tempFont); //向主界面传递该字体
this->hide();
}
void letterFormWindow::on_fontComboBox_currentFontChanged(const QFont &f)
{
tempFont.setFamily(f.family());
ChangeFont();
}
void letterFormWindow::on_spinBox_valueChanged(int arg1)
{
tempFont.setPointSize(arg1);
ChangeFont();
}
void letterFormWindow:: ChangeFont(void)
{
letterUi->label->setFont(tempFont);
}
7. Create the main interface receiving function, receive the font QFont from the font setting panel, and update the font of the text in the serial port receiving box
//接收字体窗口
void MainWindow::receiveFont(QFont font)
{
ui->uartReadPlain->setFont(font);
}
8. Declare the font window interface in the header file of the main interface (mainly to open the last opened window, not to open a new one every time)
private:
Ui::MainWindow *ui;
letterFormWindow *letterFormUi = NULL; //字体窗口
9. In this way, you can set the font, as shown below:
Only two text attributes are set here: font and size. Of course, you can set more. The methods are the same. Just add a few more components to configure.