Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why the string on model of table view will be changed after adding application font.

Why the string on model of table view will be changed after adding application font.

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 968 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mihan
    wrote on 13 Aug 2019, 08:22 last edited by
    #1

    I set the font in the model of QTableView in data()

    QFont font("Sans Serif", 12, QFont::Normal, true);
            switch (index.column()) {
            case 0:
                return QVariant();
                break;
            case 1:
                return font;
                break;
            default:
                return QVariant();
                break;
    

    but when I QFontDatabase::addApplicationFont() a new font by myself, all the strings of the column has been set font are changed to squares. This phenomenon only appears on Arm. What should I do?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 13 Aug 2019, 17:07 last edited by
      #2

      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];
          }
      }
      
      M 1 Reply Last reply 14 Aug 2019, 01:26
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 13 Aug 2019, 18:54 last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 2 Replies Last reply 14 Aug 2019, 01:15
        1
        • S SGaist
          13 Aug 2019, 18:54

          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 ?

          M Offline
          M Offline
          Mihan
          wrote on 14 Aug 2019, 01:15 last edited by
          #4

          @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 resource

              m_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.

          1 Reply Last reply
          0
          • M mrjj
            13 Aug 2019, 17:07

            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];
                }
            }
            
            M Offline
            M Offline
            Mihan
            wrote on 14 Aug 2019, 01:26 last edited by
            #5

            @mrjj Hi,
            I'm sure it was added successfully. But it is added in plugin.

            Also, I try adding this font in main()of the project but do not use it, This phenomenon appears.

            1 Reply Last reply
            0
            • S SGaist
              13 Aug 2019, 18:54

              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 ?

              M Offline
              M Offline
              Mihan
              wrote on 14 Aug 2019, 02:05 last edited by
              #6

              @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 modify QFont 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?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mihan
                wrote on 14 Aug 2019, 02:26 last edited by
                #7

                @mrjj @SGaist
                Hi
                I found the project's default font is "Sans Serif" not "WenQuanYi Micro Hei".
                But I set the font in data(),

                QFont font = QApplication::font();
                

                it's still will be changed when I add a font.

                Could you please explain?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 14 Aug 2019, 19:18 last edited by
                  #8

                  Can your create a minimal compilable example that reproduces this ?

                  By the way, which version of Qt are young using ?
                  On which platform ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply 15 Aug 2019, 02:25
                  0
                  • S SGaist
                    14 Aug 2019, 19:18

                    Can your create a minimal compilable example that reproduces this ?

                    By the way, which version of Qt are young using ?
                    On which platform ?

                    M Offline
                    M Offline
                    Mihan
                    wrote on 15 Aug 2019, 02:25 last edited by Mihan
                    #9

                    @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)

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 16 Aug 2019, 21:12 last edited by
                      #10

                      Then please, add the content of the UI file to that post.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      M 1 Reply Last reply 17 Aug 2019, 05:25
                      0
                      • S SGaist
                        16 Aug 2019, 21:12

                        Then please, add the content of the UI file to that post.

                        M Offline
                        M Offline
                        Mihan
                        wrote on 17 Aug 2019, 05:25 last edited by
                        #11

                        @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
                        
                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 17 Aug 2019, 22:08 last edited by
                          #12

                          What is the content of font.qrc ?
                          Depending on that, where do you get the corresponding file(s) ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          M 1 Reply Last reply 21 Aug 2019, 01:39
                          0
                          • S SGaist
                            17 Aug 2019, 22:08

                            What is the content of font.qrc ?
                            Depending on that, where do you get the corresponding file(s) ?

                            M Offline
                            M Offline
                            Mihan
                            wrote on 21 Aug 2019, 01:39 last edited by Mihan
                            #13

                            @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.

                            1 Reply Last reply
                            0

                            6/13

                            14 Aug 2019, 02:05

                            • Login

                            • Login or register to search.
                            6 out of 13
                            • First post
                              6/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved