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. [Solved] Set Font of QTextEdit and QListWidget

[Solved] Set Font of QTextEdit and QListWidget

Scheduled Pinned Locked Moved General and Desktop
21 Posts 4 Posters 68.9k 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.
  • K Offline
    K Offline
    kbt90
    wrote on last edited by
    #1

    Hello -

    I am attempting to set the font for both a QTextEdit and a QListWidget.

    I have tried setting the font for the QTextEdit object in the following way:

    @textEdit->setFont (QFont ("Courier", 9));@

    This did not change the font of the text displayed in textEdit.

    I also would like to change all items in the QListWidget to courier as well. I have tried setting the font of the QListWidget object, as well as each individual item to no avail.

    @lwLogList->setFont (QFont ("Courier", 9));@

    @QString temp = QString(typeStr) + " " + QString(timeStr.c_str()) + " " + QString(stateStr);
    QListWidgetItem * tmpItem = new QListWidgetItem(temp, lwLogList);
    mpItem->setFont (QFont ("Courier", 9));@

    I am unsure as to what I am doing incorrectly. Any help would be much appreciated.

    Thanks,
    Katelyn

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      You can find the explanation for that behaviour in the "QWidget docs :":http://doc.qt.nokia.com/4.7/qwidget.html#font-prop

      You can solve this by giving your whole dialog the font you want in it's constructor :
      @setFont (QFont ("Courier", 9)); @
      But I don't know if that's ok with your initial design.

      The easiest way to solve this is to use stylesheets which take precedence over setFont.
      @ui->textEdit->setStyleSheet("font: 9pt "Courier";");@
      (You can experiment with this using Qt Designer : RMB on a widget > Change Stylesheet.)

      But using stylesheets per widget doesn't use it's real force. You can set this for the whole of your application to get a look and feel which is the same everywhere.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kbt90
        wrote on last edited by
        #3

        Thanks for the reply. I am using Qt Designer to create the widget. Then I want to customize the font. I've tried changing the font in Qt Designer to Courier, but that does not work either. Since I am doing it in this way, I cannot use the constructor.

        I did try to set the style sheet for each widget in the following way:

        @lwLogList->setStyleSheet ("font: 9pt "Courier";");@

        @textEdit->setStyleSheet ("font: 9pt "Courier";");@

        But the font still remains the same as it originally was, except for the size is increased. The rest of the application needs to stay the font it currently is, just these two things need to change for display purposes.

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          Hi Katelyn,

          It should be enough to use

          @font: 9pt "Courier";
          @
          You do not need the \ in this case. From your code I could not see you used Qt Designer.

          Did you use the RMB Change stylesheet > dialog with on the right "font" which opens another dialog that let's you select everything in a gui way?

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kbt90
            wrote on last edited by
            #5

            I set the style sheet in the C++ code as instructed above. I also tried setting the style sheet within Qt Designer using the gui way you describe above. Neither seem to work. The font size gets larger, but it is still not a fixed width, which is my ultimate goal.

            Now I am writing the C++ code to design the GUI and setting the font with the constructor per your first response. I am still unable to get the font to be fixed width.

            Thanks,
            Katelyn

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              bq. The font size gets larger, but it is still not a fixed width, which is my ultimate goal.

              The suggested code only sets the font type and size. What exactly do you want to do? Fixed width? Can you explain what you want about that in it's context? You didn't mention this before.

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kbt90
                wrote on last edited by
                #7

                Well there are certain fonts where each character takes up the same amount of space (ie Courier, Courier New). I need the font to be fixed width in order to display columns that are lined up for each list item.

                I have printed out the font, and it appears as though the font is set to Courier. But, the characters are still not fixed width as they should be with Courier.

                1 Reply Last reply
                0
                • EddyE Offline
                  EddyE Offline
                  Eddy
                  wrote on last edited by
                  #8

                  That sounds strange. What os are you on? Did you try the same courier font in a texteditor and see what happens? The reason i ask is that afaik Qt uses the fonts from your system.

                  Qt Certified Specialist
                  www.edalsolutions.be

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kbt90
                    wrote on last edited by
                    #9

                    I am running a linux OS. The courier font in the text editor is also courier, but does not appear to be fixed width. I've also tried:

                    @QListWidgetItem * tmpItem = new QListWidgetItem(temp, lwLogList);
                    QFont font = QFont ("Courier");
                    font.setStyleHint (QFont::Monospace);
                    font.setPointSize (8);
                    font.setFixedPitch (true);
                    tmpItem->setFont (font);@

                    But this doesn't seem to change anything, other than some of the parameters when the font type is printed out.
                    QFont( "Courier,8,-1,2,50,0,0,0,1,0" ) - from the above code
                    vs.
                    QFont( "Courier,8,-1,5,50,0,0,0,0,0" ) - from setting the style sheet.

                    1 Reply Last reply
                    0
                    • EddyE Offline
                      EddyE Offline
                      Eddy
                      wrote on last edited by
                      #10

                      So using courier to get fixed width is not the best approach to get what you want.

                      Could you give us a screenshot of your dialog? There might be other Qt ways to get what you want.

                      Qt Certified Specialist
                      www.edalsolutions.be

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kbt90
                        wrote on last edited by
                        #11

                        Here is the code for the dialog. It has a text box and underneath is a button bar.

                        @setWindowTitle ("Title");
                        QVBoxLayout *mainLayout = new QVBoxLayout;

                        textEdit = new QTextEdit (this);
                        QFont font = QFont ("Courier");
                        font.setStyleHint (QFont::Monospace);
                        font.setPointSize (8);
                        font.setFixedPitch (true);
                        textEdit->setFont (font);

                        mainLayout->addWidget (textEdit);
                        setAutoFillBackground(true);

                        QHBoxLayout *bar = new QHBoxLayout;
                        btnBack = new QPushButton ("Back", this);
                        btnBack->setMinimumSize (QSize (80, 40));
                        bar->addWidget (btnBack);
                        btnSave = new QPushButton ("Save", this);
                        btnSave->setMinimumSize (QSize (80, 40));
                        bar->addWidget (btnSave);
                        btnPrint = new QPushButton ("Print", this);
                        btnPrint->setMinimumSize (QSize (80, 40));
                        bar->addWidget (btnPrint);
                        btnErase = new QPushButton ("Erase", this);
                        btnErase->setMinimumSize (QSize (80, 40));
                        bar->addWidget (btnErase);
                        bar->addStretch ();

                        mainLayout->addLayout (bar);
                        mainLayout->setContentsMargins (6, 6, 6, 6);
                        setLayout (mainLayout);
                        @

                        Thanks for your help,
                        Katelyn

                        1 Reply Last reply
                        0
                        • EddyE Offline
                          EddyE Offline
                          Eddy
                          wrote on last edited by
                          #12

                          As I see this, you have a QTextEdit on top and beneath it a line of four buttons.

                          I don't see the QListWidget.

                          What do you want to line up?

                          Qt Certified Specialist
                          www.edalsolutions.be

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kbt90
                            wrote on last edited by
                            #13

                            Oops - sorry about that. Here is an updated version:

                            @setWindowTitle ("Title");
                            QVBoxLayout *mainLayout = new QVBoxLayout;

                            lwListWidget = new QListWidget (this);
                            QFont font = QFont ("Courier");
                            font.setStyleHint (QFont::Monospace);
                            font.setPointSize (8);
                            font.setFixedPitch (true);
                            lwListWidget->setFont (font);

                            mainLayout->addWidget (lwListWidget);
                            setAutoFillBackground(true);

                            QHBoxLayout *bar = new QHBoxLayout;
                            btnBack = new QPushButton ("Back", this);
                            btnBack->setMinimumSize (QSize (80, 40));
                            bar->addWidget (btnBack);
                            btnSave = new QPushButton ("Save", this);
                            btnSave->setMinimumSize (QSize (80, 40));
                            bar->addWidget (btnSave);
                            btnPrint = new QPushButton ("Print", this);
                            btnPrint->setMinimumSize (QSize (80, 40));
                            bar->addWidget (btnPrint);
                            btnErase = new QPushButton ("Erase", this);
                            btnErase->setMinimumSize (QSize (80, 40));
                            bar->addWidget (btnErase);
                            bar->addStretch ();

                            mainLayout->addLayout (bar);
                            mainLayout->setContentsMargins (6, 6, 6, 6);
                            setLayout (mainLayout);@

                            Then I add items with the following code:
                            @QListWidgetItem * tmpItem = new QListWidgetItem(temp, lwListWidget); // where temp is a QString
                            QFont font = QFont ("Courier");
                            font.setStyleHint (QFont::Monospace);
                            font.setPointSize (8);
                            font.setFixedPitch (true);
                            tmpItem->setFont (font);@

                            I cannot add the items directly in Qt Designer or in the constructor because I won't know what they are at that time.

                            Thanks,
                            Katelyn

                            1 Reply Last reply
                            0
                            • EddyE Offline
                              EddyE Offline
                              Eddy
                              wrote on last edited by
                              #14

                              Ok, you have a QListWidget on top and beneath it a line of four buttons.

                              What do you want to line up?

                              Qt Certified Specialist
                              www.edalsolutions.be

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                kbt90
                                wrote on last edited by
                                #15

                                Within the QListWidget I have a list of items. These items display a name, date and a status.

                                For example:
                                JobA June 26, 2011 Completed
                                JobB July 4, 2011 In Progress

                                Names, dates and statuses are different lengths, but I would like each column to match up. I am trying a QTableView and only allowing complete rows to be selected. Does this seem like an acceptable solution?

                                1 Reply Last reply
                                0
                                • EddyE Offline
                                  EddyE Offline
                                  Eddy
                                  wrote on last edited by
                                  #16

                                  Of course, QTableview will make the columns adapt to the contents.
                                  You can make the grid not visible if you want.
                                  QTableView can be set to select/highlight an entire row.

                                  Qt Certified Specialist
                                  www.edalsolutions.be

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    kbt90
                                    wrote on last edited by
                                    #17

                                    Super - thanks so much for your help.

                                    1 Reply Last reply
                                    0
                                    • EddyE Offline
                                      EddyE Offline
                                      Eddy
                                      wrote on last edited by
                                      #18

                                      You're welcome.

                                      If you have more specific questions, feel free to ask.

                                      Qt Certified Specialist
                                      www.edalsolutions.be

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        sasireka
                                        wrote on last edited by
                                        #19

                                        i tried like this. it is working for english fonts. but it is not working for tamil fonts.
                                        @ui->label1->setStyleSheet ("font: 40pt "Lohit Tamil";");
                                        ui->label1->setText("சென்னை");
                                        //ui->label1->setText("tamil");
                                        ui->label1->show();@

                                        [quote author="Eddy" date="1311141291"]You can find the explanation for that behaviour in the "QWidget docs :":http://doc.qt.nokia.com/4.7/qwidget.html#font-prop

                                        You can solve this by giving your whole dialog the font you want in it's constructor :
                                        @setFont (QFont ("Courier", 9)); @
                                        But I don't know if that's ok with your initial design.

                                        The easiest way to solve this is to use stylesheets which take precedence over setFont.
                                        @ui->textEdit->setStyleSheet("font: 9pt "Courier";");@
                                        (You can experiment with this using Qt Designer : RMB on a widget > Change Stylesheet.)

                                        But using stylesheets per widget doesn't use it's real force. You can set this for the whole of your application to get a look and feel which is the same everywhere.
                                        [/quote]

                                        .................................
                                        Thanks & Regards

                                        Sasi

                                        .................................
                                        Go Green

                                        1 Reply Last reply
                                        0
                                        • K Offline
                                          K Offline
                                          kumararajas
                                          wrote on last edited by
                                          #20

                                          Hi Sasireka,

                                          Do you have tamil font installed in your computer?

                                          --Kumar

                                          1 Reply Last reply
                                          0

                                          • Login

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