3

I need to create a table like this

table

how can I align first column left, 2nd and 3rd columns center, and last column right sothat when someone put data in, it is automatically aligned as I want ?

My first code:

ui->tableWidget->setColumnCount(4);
ui->tableWidget->setRowCount(52);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << tr("Name") << tr("Age") << tr("Position") << tr("Salary"));

I know we have something like

ui.tableWidget->item(i, 0)->setTextAlignment(Qt::AlignLeft);

But i don't know how to set it for every row of a column.

Many thanks !

8
  • Maybe use a real model (and a QTableView) so that you can return the right alignment for the alignment role.
    – peppe
    Jun 27, 2016 at 8:09
  • @maxik: thanks for your hint. I tried with that already but it doesnt work. They program a table and they put data in it also. Here I program a table for other people to put data.
    – trangan
    Jun 27, 2016 at 8:53
  • @peppe: thank you. I just have learnt Qt for a week. I dont understand your idea. What do you mean by "real model" ? Could you give me a hint with code ?
    – trangan
    Jun 27, 2016 at 8:55
  • @htmlamateur you might go with peppe's answer or you read the second link more closely: stackoverflow.com/a/15829092/6385043 there it describes a factory implementation for item creation. Or subclassing the QTableWidgetItem itself. A plenty of possibilities to solve this.
    – maxik
    Jun 27, 2016 at 8:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.