[Solved] Set Font of QTextEdit and QListWidget



  • 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


  • Moderators

    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.



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


  • Moderators

    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?



  • 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


  • Moderators

    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.



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


  • Moderators

    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.



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


  • Moderators

    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.



  • 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


  • Moderators

    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?



  • 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


  • Moderators

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

    What do you want to line up?



  • 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?


  • Moderators

    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.



  • Super - thanks so much for your help.


  • Moderators

    You're welcome.

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



  • 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]



  • Hi Sasireka,

    Do you have tamil font installed in your computer?



  • "This question has been solved here: ":http://qt-project.org/forums/viewthread/47761/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.