Qt:标准字体对话框类QFontDialog选择字体设置文本编辑器

迷南。 2023-01-12 11:42 609阅读 0赞

1、新建”QT GUI”,基类选择”QDialog’,取消“创建界面”

2、dialog.h中

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include <QDialog>
  4. #include <QLineEdit>
  5. #include <QPushButton>
  6. #include <QGridLayout>
  7. class Dialog : public QDialog
  8. {
  9. Q_OBJECT
  10. public:
  11. Dialog(QWidget *parent = 0);
  12. ~Dialog();
  13. private:
  14. QPushButton *fontBtn;
  15. QLineEdit *fontLineEdit;
  16. QGridLayout *mainLayout;
  17. private slots:
  18. void showFont();
  19. };
  20. #endif // DIALOG_H

2、dialog.cpp中

  1. #include "dialog.h"
  2. #include <QFontDialog>
  3. Dialog::Dialog(QWidget *parent)
  4. : QDialog(parent)
  5. {
  6. //控件初始化
  7. fontBtn = new QPushButton;
  8. fontBtn->setText(tr("字体标准对话框"));
  9. fontLineEdit = new QLineEdit;
  10. fontLineEdit->setText(tr("welcome!"));
  11. //添加布局管理
  12. mainLayout = new QGridLayout(this); //注意一定要有一个this
  13. mainLayout->addWidget(fontBtn, 2, 0);
  14. mainLayout->addWidget(fontLineEdit, 2, 1);
  15. //信号与槽
  16. connect(fontBtn, SIGNAL(clicked()), this, SLOT(showFont()));
  17. }
  18. Dialog::~Dialog()
  19. {
  20. }
  21. void Dialog::showFont()
  22. {
  23. bool ok;
  24. QFont f = QFontDialog::getFont(&ok);
  25. if(ok){
  26. fontLineEdit->setFont(f);
  27. }
  28. }

3、效果

字体对话框选择

QFont getFont
(
bool *ok; //如果”ok”,返回用户选择的字体,否则,返回函数默认字体
QWidget *parent = 0;
);

发表评论

表情:
评论列表 (有 0 条评论,609人围观)

还没有评论,来说两句吧...

相关阅读