You are not logged in.

1

Wednesday, June 29th 2011, 3:57am

QTableWidgetItem some problems

I was writing a simple table code. I want to align the text in center. I tried using

cell(row, column)->setTextAlignment(Qt::AlignRight);

but it does not work. The text stays aligned to left in the cell.

Also, another question. I know that QTableWidget has show grid slot. I implemented the slot using a toggled(bool) signal with a menu action. However, I dont notice any change.

Here is the part of code..

Source code

1
2
3
4
5
6
7
8
9
10
11
12
void Spreadsheet:: alignRight() {
	QTableWidgetSelectionRange range = selectedRange();

	for (int i=0; i<range.rowCount(); ++i) {
		for (int j=0; j<range.columnCount(); ++j) {
			Cell *c = cell(range.topRow() + i, range.leftColumn() +j);
			c->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
			c->setDirty();
		}
	}
	viewport()->update();
}


for showgrid..

Source code

1
2
3
4
5
	showGridAction = new QAction(tr("&Show Grid"), this);
	showGridAction->setCheckable(true);
	showGridAction->setChecked(spreadsheet->showGrid());
	showGridAction->setStatusTip(tr("Show or hide spreadsheet's grid"));
	connect(showGridAction, SIGNAL(toggled(bool)), this, SLOT(setShowGrid(bool)));

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Wednesday, June 29th 2011, 2:55pm

Nick,

// Alignment
// Check to ensure c pointer is referencing type tableWidgetItem (might have to cast).
c->setTextAlignment(Qt::AlignCenter);

// connection
// (this) should be the tableWidget
connect(showGridAction, SIGNAL(toggled(bool)), tableWidget, SLOT(setShowGrid(bool)));

3

Thursday, June 30th 2011, 3:08am

Thanks!! The show grid issue is resolved, but the alignment issue exists.

cell(row, column) method returns an object of an extended class of QTableWidgetItem.

I still dont understand why setTextAlignment doesnt work.

Junior

Professional

  • "Junior" is male

Posts: 1,622

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

4

Thursday, June 30th 2011, 1:26pm

If your just trying to center text that are selected, then maybe some like this would be something to consider:

Source code

1
2
3
4
5
6
void SpreadSheet::alignCenter()
{
    foreach( QTableWidgetItem *item, tableWidget->selectedItems() ){
        item->setTextAlignment(Qt::AlignCenter);
    } 
}


The SelectionRange only provide positional references of selected items and not access to the items themselves.

Just a thought.

Note on possible reason for not setting text alignment using cell(); without seeing how cell() code it might be that it's creating a copy(?) and it is setting the alignment but never shown because the item in the table is not being referenced and the imposter is destroyed when leaving the body of the function? Just a guess without seeing the code.

This post has been edited 1 times, last edit by "Junior" (Jun 30th 2011, 1:32pm)


5

Friday, July 1st 2011, 1:58am

NO luck!!

I tried your code. It dint work. By default, the text is aligned to left and numbers to the right.

The cell(row, column) just calls the item(row, method). It returns QTableWidgetItem * as well..

6

Friday, July 1st 2011, 3:50am

Hey Thanks!!

I figured it out. Apparently it was my own bug. So when I m setting up to display the value in string, I set the default numbers to right and strings to left, thats why it was always getting overwritten irrespective of what I set it to. But now it works fine..

Thanks..