Why the string on model of table view will be changed after adding application font.
-
Hi,
To add to @mrjj, did you deploy the font on the device ? Or are you embedding it in the application through Qt's resources system ?
@sgaist Hi,
I add AwesomeFont in the vritual keyboard I made, the vritual keyboard is a plugin. When it is shown, it will add the font in resourcem_FontId = QFontDatabase::addApplicationFont(":/font/FontAwesome.otf"); QString fontName = QFontDatabase::applicationFontFamilies(m_FontId).at(0); QFont iconFont(fontName); iconFont.setPixelSize(20);
At the same time, the string of table in main project will be changed to be squares. Also, if I don't set the font in
data()
of table model, this phenomenon will not happen. -
Hi
Sounds like something wrong with font.
Try testing if it really is added after calling QFontDatabase::addApplicationFont()QFontDatabase fdb; QStringList fam = fdb.families(); qDebug() << "QFontDatabase::Any: available font families -> " << fam.count(); for (int i = 0 ; i < fam.count() ; i++) { qDebug() << fam[i]; } }
-
Hi,
To add to @mrjj, did you deploy the font on the device ? Or are you embedding it in the application through Qt's resources system ?
@sgaist @mrjj
Thank you so much.
I found there are only two fonts except I added in the system .QFontDatabase::Any: available font families -> 3 "FontAwesome" "WenQuanYi Micro Hei" "WenQuanYi Micro Hei Mono"
So when I set font to be "Sans Serif". It can't find this font and will use the first font of family default, "WenQuanYi Micro Hei". And when I add Awesome font, the font of string in the table I had set will be "WenQuanYi Micro Hei" -> "FontAwesome" .
So I should add "Sans Serif" in the system or modifyQFont font("Sans Serif", 12, QFont::Normal, true);
->QFont font("WenQuanYi Micro Hei", 12, QFont::Normal, true);
Is that right? Or can I know which font the system use default?
-
Can your create a minimal compilable example that reproduces this ?
By the way, which version of Qt are young using ?
On which platform ? -
Can your create a minimal compilable example that reproduces this ?
By the way, which version of Qt are young using ?
On which platform ?@sgaist
Sure. Here's an example. When you click the button ,it will add the font, then you should touch the table to make it updated , and you can see the phenomenon. If you don't add font, the phenomenon will not appear.I'm using Qt5.5.0 for linux, Qt5.6.2 for Arm-linux. This phenomenon only appears on Arm-linux. The arm-linux system has only two font
uh....."you do not have enough privileges for this action.", I can't upload file. So....
#mainwindows.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> #include <QStringList> #include <QApplication> #include <QFontDatabase> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); InitTableModel(); ConnectSlots(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::InitTableModel() { TestModel *model = new TestModel; ui->tableView->setModel(model); } void MainWindow::ConnectSlots() { connect(ui->CteateBtn,SIGNAL(clicked(bool)),this,SLOT(CreateFont())); } void MainWindow::CreateFont() { qDebug()<<"add awesome font"; QFontDatabase::addApplicationFont(":/new/prefix1/FontAwesome.otf"); } TestModel::TestModel(QObject *parent): QAbstractTableModel(parent) { m_String.clear(); m_String<<"test1"<<"test2"<<"test3"<<"test4"<<"test5"<<"test6"<<"test7"; } TestModel::~TestModel() { } int TestModel::columnCount(const QModelIndex &parent) const { return 2; } int TestModel::rowCount(const QModelIndex &parent) const { return m_String.count(); } QVariant TestModel::data(const QModelIndex &index, int role) const { if(!index.isValid()) return QVariant(); if(role == Qt::DisplayRole) { return m_String.at(index.row()); } else if(role == Qt::FontRole) { QFont font = QApplication::font(); font.setItalic(true); switch (index.column()) { case 0: return QVariant(); break; case 1: return font; break; default: return QVariant(); break; } } else return QVariant(); }
#mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QAbstractTableModel> namespace Ui { class MainWindow; class TestModel; } class TestModel : public QAbstractTableModel { Q_OBJECT public: explicit TestModel(QObject *parent = 0); ~TestModel(); int columnCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; private: QStringList m_String; }; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void InitTableModel(); void ConnectSlots(); public slots: void CreateFont(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
There is a TableView and a Pushbutton in ui(
mainwindow.ui
) -
Then please, add the content of the UI file to that post.
-
@sgaist sorry I forgot it.
#mainwindow.ui <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>344</width> <height>469</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QTableView" name="tableView"> <attribute name="horizontalHeaderVisible"> <bool>false</bool> </attribute> <attribute name="verticalHeaderVisible"> <bool>false</bool> </attribute> </widget> </item> <item row="1" column="0"> <widget class="QPushButton" name="CteateBtn"> <property name="text"> <string>Create Font</string> </property> </widget> </item> </layout> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
#main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
# .pro #------------------------------------------------- # # Project created by QtCreator 2019-08-15T08:51:22 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = FontTest TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui RESOURCES += \ font.qrc
-
What is the content of font.qrc ?
Depending on that, where do you get the corresponding file(s) ? -
What is the content of font.qrc ?
Depending on that, where do you get the corresponding file(s) ?@sgaist I'm so sorry to reply to you after so long.
The font.qrc is a resource file I made, with the FontAwesome.otf .
It's found in a topic about making Qt virtual keyboard by BaiDu.
It is used to show some special symbols like circle, star and so on.
Maybe you can download the analogous from www.fontawesome.com.