Qt 设置字体 字号等格式属性
2012-03-04 09:17
489 查看
#ifndef FONTSET_H
#define FONTSET_H
#include <QtGui>
class FontSet : public QMainWindow
{
Q_OBJECT
public:
FontSet(QWidget *parent = 0);
QLabel *label1;
QLabel *label2;
QFontComboBox *fontBox;
QComboBox *sizeBox;
QToolButton *boldBtn;
QToolButton *italicBtn;
QToolButton *underBtn;
QToolButton *colorBtn;
void mergeFormat(QTextCharFormat);
public slots:
void slotFont(QString);
void slotSize(QString);
void slotBold();
void slotItalic();
void slotUnder();
void slotColor();
void slotCurrentFormatChanged(const QTextCharFormat &fmt);
private:
QTextEdit *text;
};
#endif // FONTSET_H
/** fontset.cpp
#include "fontset.h"
#include <QtGui>
FontSet::FontSet(QWidget *parent)
: QMainWindow(parent)
{
QFont f("ZYSong18030", 12);
setFont(f);
setWindowTitle(tr("Font"));
QToolBar *toolBar = addToolBar(tr("Font"));
QLabel *label1 = new QLabel(tr("Font: "));
fontBox = new QFontComboBox(toolBar);
fontBox->setFontFilters(QFontComboBox::ScalableFonts);
toolBar->addWidget(label1);
toolBar->addWidget(fontBox);
QLabel *label2 = new QLabel(tr("number: "));
sizeBox = new QComboBox(toolBar);
toolBar->addWidget(label2);
toolBar->addWidget(sizeBox);
QFontDatabase db;
foreach(int size, db.standardSizes())
{
sizeBox->addItem(QString::number(size));
}
toolBar->addSeparator();
boldBtn = new QToolButton;
boldBtn->setIcon(QIcon(":/images/bold.png"));
boldBtn->setCheckable(true);
italicBtn = new QToolButton;
italicBtn->setIcon(QIcon(":/images/italic.png"));
italicBtn->setCheckable(true);
toolBar->addWidget(boldBtn);
toolBar->addWidget(italicBtn);
underBtn = new QToolButton;
underBtn->setIcon(QIcon(":/images/underline.png"));
underBtn->setCheckable(true);
toolBar->addWidget(underBtn);
toolBar->addSeparator();
colorBtn = new QToolButton;
colorBtn->setIcon(QIcon(":/images/color.png"));
toolBar->addWidget(colorBtn);
text = new QTextEdit(this);
text->setFocus();
setCentralWidget(text);
connect(fontBox, SIGNAL(activated(QString)), this, SLOT(slotFont(QString)));
connect(sizeBox, SIGNAL(activated(QString)), this, SLOT(slotSize(QString)));
connect(boldBtn, SIGNAL(clicked()),this, SLOT(slotBold()));
connect(italicBtn, SIGNAL(clicked()), this, SLOT(slotItalic()));
connect(underBtn, SIGNAL(clicked()), this, SLOT(slotUnder()));
connect(colorBtn, SIGNAL(clicked()), this, SLOT(slotColor()));
connect(text, SIGNAL(currentCharFormatChanged(const QTextCharFormat&)), this, SLOT(slotCurrentFormatChanged(const QTextCharFormat&)));
}
void FontSet::slotFont(QString f)
{
QTextCharFormat fmt;
fmt.setFontFamily(f);
mergeFormat(fmt);
}
void FontSet::slotSize(QString num)
{
QTextCharFormat fmt;
fmt.setFontPointSize(num.toFloat());
mergeFormat(fmt);
}
void FontSet::slotBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(boldBtn->isChecked()? QFont::Bold : QFont::Normal);
mergeFormat(fmt);
}
void FontSet::slotItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(italicBtn->isChecked());
mergeFormat(fmt);
}
void FontSet::slotUnder()
{
QTextCharFormat fmt;
fmt.setFontUnderline(underBtn->isChecked());
mergeFormat(fmt);
}
void FontSet::slotColor()
{
QColor color = QColorDialog::getColor (Qt::red,this);
if(color.isValid())
{
QTextCharFormat fmt;
fmt.setForeground(color);
mergeFormat(fmt);
}
}
// 设置光标的选区,使格式作用于选区内的字符,若没有选区则作用于光标所在处的字符
void FontSet::mergeFormat(QTextCharFormat format)
{
QTextCursor cursor = text->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
text->mergeCurrentCharFormat(format);
}
void FontSet::slotCurrentFormatChanged(const QTextCharFormat &fmt)
{
fontBox->setCurrentIndex(fontBox->findText(fmt.fontFamily()));
sizeBox->setCurrentIndex(sizeBox->findText(QString::number(fmt.fontPointSize())));
boldBtn->setChecked(fmt.font().bold());
italicBtn->setChecked(fmt.fontItalic());
underBtn->setChecked(fmt.fontUnderline());
}
/** main.cpp
#include <QApplication>
#include "fontset.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QTranslator translator;
translator.load("fonts_zh");
app.installTranslator(&translator);
FontSet *t = new FontSet();
t->show();
return app.exec();
}
#define FONTSET_H
#include <QtGui>
class FontSet : public QMainWindow
{
Q_OBJECT
public:
FontSet(QWidget *parent = 0);
QLabel *label1;
QLabel *label2;
QFontComboBox *fontBox;
QComboBox *sizeBox;
QToolButton *boldBtn;
QToolButton *italicBtn;
QToolButton *underBtn;
QToolButton *colorBtn;
void mergeFormat(QTextCharFormat);
public slots:
void slotFont(QString);
void slotSize(QString);
void slotBold();
void slotItalic();
void slotUnder();
void slotColor();
void slotCurrentFormatChanged(const QTextCharFormat &fmt);
private:
QTextEdit *text;
};
#endif // FONTSET_H
/** fontset.cpp
#include "fontset.h"
#include <QtGui>
FontSet::FontSet(QWidget *parent)
: QMainWindow(parent)
{
QFont f("ZYSong18030", 12);
setFont(f);
setWindowTitle(tr("Font"));
QToolBar *toolBar = addToolBar(tr("Font"));
QLabel *label1 = new QLabel(tr("Font: "));
fontBox = new QFontComboBox(toolBar);
fontBox->setFontFilters(QFontComboBox::ScalableFonts);
toolBar->addWidget(label1);
toolBar->addWidget(fontBox);
QLabel *label2 = new QLabel(tr("number: "));
sizeBox = new QComboBox(toolBar);
toolBar->addWidget(label2);
toolBar->addWidget(sizeBox);
QFontDatabase db;
foreach(int size, db.standardSizes())
{
sizeBox->addItem(QString::number(size));
}
toolBar->addSeparator();
boldBtn = new QToolButton;
boldBtn->setIcon(QIcon(":/images/bold.png"));
boldBtn->setCheckable(true);
italicBtn = new QToolButton;
italicBtn->setIcon(QIcon(":/images/italic.png"));
italicBtn->setCheckable(true);
toolBar->addWidget(boldBtn);
toolBar->addWidget(italicBtn);
underBtn = new QToolButton;
underBtn->setIcon(QIcon(":/images/underline.png"));
underBtn->setCheckable(true);
toolBar->addWidget(underBtn);
toolBar->addSeparator();
colorBtn = new QToolButton;
colorBtn->setIcon(QIcon(":/images/color.png"));
toolBar->addWidget(colorBtn);
text = new QTextEdit(this);
text->setFocus();
setCentralWidget(text);
connect(fontBox, SIGNAL(activated(QString)), this, SLOT(slotFont(QString)));
connect(sizeBox, SIGNAL(activated(QString)), this, SLOT(slotSize(QString)));
connect(boldBtn, SIGNAL(clicked()),this, SLOT(slotBold()));
connect(italicBtn, SIGNAL(clicked()), this, SLOT(slotItalic()));
connect(underBtn, SIGNAL(clicked()), this, SLOT(slotUnder()));
connect(colorBtn, SIGNAL(clicked()), this, SLOT(slotColor()));
connect(text, SIGNAL(currentCharFormatChanged(const QTextCharFormat&)), this, SLOT(slotCurrentFormatChanged(const QTextCharFormat&)));
}
void FontSet::slotFont(QString f)
{
QTextCharFormat fmt;
fmt.setFontFamily(f);
mergeFormat(fmt);
}
void FontSet::slotSize(QString num)
{
QTextCharFormat fmt;
fmt.setFontPointSize(num.toFloat());
mergeFormat(fmt);
}
void FontSet::slotBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(boldBtn->isChecked()? QFont::Bold : QFont::Normal);
mergeFormat(fmt);
}
void FontSet::slotItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(italicBtn->isChecked());
mergeFormat(fmt);
}
void FontSet::slotUnder()
{
QTextCharFormat fmt;
fmt.setFontUnderline(underBtn->isChecked());
mergeFormat(fmt);
}
void FontSet::slotColor()
{
QColor color = QColorDialog::getColor (Qt::red,this);
if(color.isValid())
{
QTextCharFormat fmt;
fmt.setForeground(color);
mergeFormat(fmt);
}
}
// 设置光标的选区,使格式作用于选区内的字符,若没有选区则作用于光标所在处的字符
void FontSet::mergeFormat(QTextCharFormat format)
{
QTextCursor cursor = text->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
text->mergeCurrentCharFormat(format);
}
void FontSet::slotCurrentFormatChanged(const QTextCharFormat &fmt)
{
fontBox->setCurrentIndex(fontBox->findText(fmt.fontFamily()));
sizeBox->setCurrentIndex(sizeBox->findText(QString::number(fmt.fontPointSize())));
boldBtn->setChecked(fmt.font().bold());
italicBtn->setChecked(fmt.fontItalic());
underBtn->setChecked(fmt.fontUnderline());
}
/** main.cpp
#include <QApplication>
#include "fontset.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QTranslator translator;
translator.load("fonts_zh");
app.installTranslator(&translator);
FontSet *t = new FontSet();
t->show();
return app.exec();
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/202008/11/c232f7986e236bdd990c5ff9576525f0.jpg)
相关文章推荐
- 【PyQt实例3】设置字体,字号等格式属性
- 设置字体、字号等格式属性
- iOS-详解文本属性attributes,设置段落格式,空心字体
- qt4.7 字体字号设设置;中文乱码,
- Qt学习笔记2014.7.17.14:16——下拉框使用以及设置文字字体、字号
- 设置QT标准提示对话框中字体格式
- qt4.7 字体字号设设置;中文乱码,
- Endnote 参考文献格式设置:字体 字号 行距 悬挂缩进
- 设置输入框input中placeholder属性的字体颜色、字号
- OpenFileDialog Filter属性设置所有图片格式
- Android中Paint字体属性的设置
- Qt中设置QToolBar工具条不可拖动属性
- 如何设置WINCE系统字体、字号?如何设置自己开发的软件的字体、字号
- ActionScript 3.0 学习(七) AS3 TextFormat及StyleSheet设置文本类TextField字体格式
- POI中设置Excel单元格格式样式(居中,字体,边框,背景色、列宽、合并单元格等)
- QT中字体和颜色的设置
- Android SpannableString设置超链接、颜色、字体等属性
- dataGridView设置数据字体格式
- eclipse使用教程系列五:调优,启动参数设置,字体,背景颜色,编码格式等常用设置大全
- 设置不同的android:inputTye属性时,如果修改EditText hint 的字体