PDA

View Full Version : How to get text with outlines



bercker
12th June 2020, 11:25
Hi guys,

i have a question:
I have a table

QTableWidget* m_ErgebnisTabelle;

This table will be shown in the GUI in a tabWidget.
In this itembased table , i will show some result values of some calculations.

QTableWidgetItem * protoitem = new QTableWidgetItem(aktuellesItem);

The text os white. But i want a text with a black outline.
I found a solution: https://www.qtcentre.org/threads/34111-QPushButton-Outline-white-text-in-black-on-transparent-background
In this solution a paintevent is used, but with a defined text. i have different values after every calculation.
And this post is 9 years old, maybe there is now another way to handle this - i dont know.

Here some code snippets, of how i setup and plot the table. The only thing missing, is the outlines.



void Ergebnisse::setupLayout()
{
m_ergebnisLayout = new QVBoxLayout();

QTabWidget* tabWidget = new QTabWidget();
m_ergebnisLayout->addWidget(tabWidget);

m_ErgebnisTabelle = new QTableWidget(1, 3, this);
QStringList header = {"ID","Ergebnis A","Ergebnis B"};
m_ErgebnisTabelle->setHorizontalHeaderLabels(header);
m_ErgebnisTabelle->verticalHeader()->hide();

QVBoxLayout* tab_vLayout = new QVBoxLayout();
tab_vLayout->addWidget(m_ErgebnisTabelle);

QWidget* tableTab = new QWidget();
tableTab->setLayout(tab_vLayout);
tabWidget->addTab(tableTab, "Tabelle");

.... more tabs ....
}



void Ergebnisse::plotErgebnisTabelle(QStringList header)
{
m_ErgebnisTabelle->clear();
int numberItems = m_alleErgebnisse.size();

int rowCount = 0;
QMapIterator<int,QPair<double,double>> iter_erg(m_alleErgebnisse);
while (iter_erg.hasNext()){
iter_erg.next();
for(int i = 0; i < header.size(); i++){
QString aktuellesItem;
if(i == 0){
aktuellesItem = QString::number(iter_erg.key());
}
else if(i == 1)
{
double aktuellerWert = iter_erg.value().first;
aktuellesItem = (QString::number(aktuellerWert));
}
else
{
double aktuellerWert = iter_erg.value().second;
aktuellesItem = (QString::number(aktuellerWert));
}

QTableWidgetItem * protoitem = new QTableWidgetItem(aktuellesItem);
protoitem->setTextAlignment(Qt::AlignCenter);
m_ErgebnisTabelle->setItem(rowCount,i,protoitem);
}
rowCount++;
}
}

d_stranz
12th June 2020, 15:33
The text os white. But i want a text with a black outline.

Are you talking about the grid lines in the table? Look at QTableView::gridStyle() and QTableView::showGrid(). If you want to draw specific cells in your table with an outline around the text (to highlight them or give special emphasis), you will probably have to create a custom QAbstractItemDelegate to draw the text they way you want. I am not sure you can do that easily with a QTableWidget, but you can with a QTableView and QAbstractTableModel.

bercker
15th June 2020, 09:11
Thanks for your answer.
I want to outline the text, not the grid lines.
So instead of a QTableWidgetItem, i have to try it with a AbstractItemDelegate ?

d_stranz
15th June 2020, 16:14
So instead of a QTableWidgetItem, i have to try it with a AbstractItemDelegate ?

QTableWidget / QTableWidgetItem are there for convenience, when you just want to display text or something else that is simple, so you don't have to go to the work of using a QTableView and QAbstractTableModel. If you want something fancier than what QTableWidgetItem can do, you probably need a delegate. Look at the Stars Delegate example for how to implement customized painting for a cell that persists after the user has stopped editing the cell.


If your table is display- (read-) only (or the cells you want to highlight are display only), then you may be able to use a cell widget (QTableWidget::setCellWidget()) containing a QLabel. QLabel inherits from QFrame, which allow you to place a border around it.