前の投稿:
QTはシリアルデバッグアシスタントを実装しています(6):ページレイアウト
一般的なシリアルポートデバッグツールには、シリアルポート受信ボックスのフォントを変更するための機能パネルがあります。1つ追加しましょう。
1.最初にフォント設定パネルを作成します
たとえば、ウィジェットを作成してletterFormWindowクラスという名前を付けると、QTcreatorは.cpp、.h、および.uiファイルを自動的に生成します。
2.図に示すように、いくつかのコンポーネントをUIインターフェイスに追加します。3
。次に、メインインターフェイスファイルの書き込みに戻り、メインインターフェイスのコマンドバーにショートカットボタンを追加します。
QMenuBar *menuBar = ui->menuBar;
QAction *letterPanlAct = menuBar->addAction("字体设置");
4.このショートカットボタンをletterFormWindowウィンドウの作成にバインドするので、ショートカットキーをクリックしてフォント設定パネルを呼び出します。
connect(letterPanlAct,&QAction::triggered,
[=] ()
{
if(letterFormUi == NULL)
{
letterFormUi = new letterFormWindow;
connect(letterFormUi, SIGNAL(sendFont(QFont)), this, SLOT(receiveFont(QFont)));
}
letterFormUi->show();
}
);
5.この文に注意してくださいconnect(letterFormUi、SIGNAL(sendFont(QFont))、this、SLOT(receiveFont(QFont)))、サブインターフェイスのsendFont関数イベントをメインインターフェイスのreceiveFont関数にバインドします。サブインターフェイスを接続するために使用されます。設定されたフォントは、変更のためにメインインターフェイスに転送されます。
6.サブインターフェースの実現:ヘッダーファイル
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ファイル:
#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.メインインターフェース受信機能を作成し、フォント設定パネルからフォントQFontを受信し、シリアルポート受信ボックスのテキストのフォントを更新します。
//接收字体窗口
void MainWindow::receiveFont(QFont font)
{
ui->uartReadPlain->setFont(font);
}
8.メインインターフェイスのヘッダーファイルでフォントウィンドウインターフェイスを宣言します(主に最後に開いたウィンドウを開くためであり、毎回新しいウィンドウを開くためではありません)。
private:
Ui::MainWindow *ui;
letterFormWindow *letterFormUi = NULL; //字体窗口
9.このようにして、以下に示すようにフォントを設定できます。
ここでは、フォントとサイズの2つのテキスト属性のみを設定します。もちろん、さらに設定することもできます。方法は同じです。構成するコンポーネントをいくつか追加するだけです。