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. How include "qfont" in "qlabel" ??

How include "qfont" in "qlabel" ??

Scheduled Pinned Locked Moved Solved General and Desktop
qfontqlabel
5 Posts 4 Posters 1.3k Views 2 Watching
  • 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.
  • timob256T Offline
    timob256T Offline
    timob256
    wrote on last edited by
    #1

    I can't understand why I can't transfer the font to the label

    .h

    #include <QFont>
    
    ...
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    ...
        // подстраиваемый шрифт label
        QFont* font;
        int height_monitor {0};
        int width_monitor  {0};
    
    ...
    
    }
    

    .cpp

    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        // тут настраиваю и объявляю элементы
    
        font = new QFont();
        ...
    }
    
    
    void MainWindow::resizeEvent(QResizeEvent *e)
    {
          height_monitor= e->size().height();
          width_monitor = e->size().width();
    
          font->setPointSize((height_monitor*width_monitor)/4);
          
          ui->label->setFont(font);  // вот тут не работает !!!!!
    }
    
    SGaistS 1 Reply Last reply
    0
    • timob256T timob256

      @SGaist hello, Alas , I did not understand which command to write to make it work .😞

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #5

      @timob256 said in How include "qfont" in "qlabel" ??:

      I did not understand which command to write to make it work

      Open documentation for setFont: https://doc.qt.io/qt-6/qwidget.html#font-prop
      As you can clearly see setFont does not take a pointer to QFont, but you're trying to give it a pointer. This is basic C++ knowledge.
      Change to:

      ui->label->setFont(*font);
      

      Or, even better, don't declare font as pointer...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • timob256T timob256

        I can't understand why I can't transfer the font to the label

        .h

        #include <QFont>
        
        ...
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        ...
            // подстраиваемый шрифт label
            QFont* font;
            int height_monitor {0};
            int width_monitor  {0};
        
        ...
        
        }
        

        .cpp

        #include "mainwindow.h"
        #include "./ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            // тут настраиваю и объявляю элементы
        
            font = new QFont();
            ...
        }
        
        
        void MainWindow::resizeEvent(QResizeEvent *e)
        {
              height_monitor= e->size().height();
              width_monitor = e->size().width();
        
              font->setPointSize((height_monitor*width_monitor)/4);
              
              ui->label->setFont(font);  // вот тут не работает !!!!!
        }
        
        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Because you declare and try to use a pointer to a QFont object while setFont takes an object.

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

        timob256T 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Because you declare and try to use a pointer to a QFont object while setFont takes an object.

          timob256T Offline
          timob256T Offline
          timob256
          wrote on last edited by
          #3

          @SGaist hello, Alas , I did not understand which command to write to make it work .😞

          JonBJ jsulmJ 2 Replies Last reply
          0
          • timob256T timob256

            @SGaist hello, Alas , I did not understand which command to write to make it work .😞

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @timob256 Don't you get a compiler error on statement ui->label->setFont(font); which tells you what is wrong?

            1 Reply Last reply
            0
            • timob256T timob256

              @SGaist hello, Alas , I did not understand which command to write to make it work .😞

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @timob256 said in How include "qfont" in "qlabel" ??:

              I did not understand which command to write to make it work

              Open documentation for setFont: https://doc.qt.io/qt-6/qwidget.html#font-prop
              As you can clearly see setFont does not take a pointer to QFont, but you're trying to give it a pointer. This is basic C++ knowledge.
              Change to:

              ui->label->setFont(*font);
              

              Or, even better, don't declare font as pointer...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • timob256T timob256 has marked this topic as solved on

              • Login

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