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