PDA

View Full Version : QTableWidget Problem, setRowWidget? :P



VireX
5th April 2007, 06:36
Ok, well I want to create a sort of list such as this:

[information1] [otherinfo1] [somemoreinfo1] [button1] [othbutton1]
[information2] [otherinfo2] [somemoreinfo2] [button2] [othbutton2]

So I made a TableWidget, because it has columns and you can add Widgets inside a cell.
But let's say I am making a list of 30 of these items. I would use some QList<QTableWidgetItem *>, and loop through these. Seems all perfect, of course, until someone clicks on a header to sort it. I mean I want to sort them, but I don't want each column to be separate from their row.

In other words, if someone presses sort by otherinfo... then let's say otherinfo3 comes first? well then the cell next to it should be information3 and somemoreinfo3... Do you see what I mean?

How would I fix that?

guilugi
5th April 2007, 10:12
You must use QTableView methods to be able to fix this :)

With QTableView, you can access to the header widgets : see QTableView::verticalHeader() / horizontalHeader().

These headers emit useful signals you can connect to : for example, sectionClicked(int).

You could a slot to this signal, that would sort all your other columns, according to the one that was clicked !

So it would be something like this :


// Signals Init

void Class::initSignals()
{
connect (tablewidget->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(sortAllColumns(int)) );
}

void Class::sortAllColumns(int)
{
// Code for sorting ! :)
}

VireX
5th April 2007, 18:58
I don't believe I have to do that.
QtableWidget has all the TableView functions.
Plus, I need setCellWidget in QTableWidget...

Any ideas on how I could sort them all together, so they won't separate from each other. Like Button1 and information1 won't separate when sorted ? Anyone have an example?

guilugi
5th April 2007, 22:16
Sure sure...I didn't want you to use a QTableview object, you can of course access those methods through QTableWidget :)

wysota
5th April 2007, 23:01
Could you define "separate"?

guilugi
5th April 2007, 23:30
I think he wants to sort the whole tablewidget...in accordance to the column that has been clicked (and sorted)

wysota
6th April 2007, 01:10
Ok, but what is the difference between "whole tablewidget" and the effect he currently experiences?

VireX
6th April 2007, 02:02
Well If I were to sort a column, it would sort ONLY that column, not ALL columns, so
If i sort Information1:

Information 1 AND [otherinfo1] [somemoreinfo1] [button1] [othbutton1]
will be in separate rows... So what am I suppose to do?

I'm not sure how I would override the column sorting like that.

VireX
6th April 2007, 05:49
Did you guys understand my problem?

In other words, each column item in each row needs to LOCK onto the columns next to it.

Info | Button
1)[info1][button1]
2)[info2][button2]
3)[info3][button3]

Then someone clicks Button header item. Then suddenly, it becomes:

Info | Button
1)[info1][button3]
2)[info2][button2]
3)[info3][button1]


But this will make button3 and info1 next to each other, which is incorrect.

I want it so that when someone clicks "Button" header item.
It sorts like this:

Info | Button
1)[info3][button3]
2)[info2][button2]
3)[info1][button1]

So in other words the numbers need to be locked together. Is this any clearer?

wysota
6th April 2007, 08:43
Did you write the sorting code yourself? The table view sorts rows by default, not columns. Maybe the effect is caused by the fact, that you use cell widgets? In that case you'll probably have to move the widgets to appropriate cells yourself.

guilugi
6th April 2007, 08:45
Yes, I understand your problem :

- You click on a column header to sort data, and you also want the other columns to be adjusted, in order to keep your rows the same ! :)

- If you really want to keep QTableWidget, I think you should :

1) deactivate the default sorting method with setSortingEnabled(false)
2) connect a custom sorting slot to the header signal sectionClicked()
3) implement your custom sorting function to sort just like you want

VireX
6th April 2007, 08:52
Thanks for the help guys. However, I tried a small test with random text in the cells, and it turns out wysota is right, it is automatically locked... I am so shocked, I tested it before and I coulda sworn it wasn't like that! I cannot believe it... But I definitely belong to this forum for another month now :).

VireX
6th April 2007, 15:53
for(int i = 1; i < 53; i++){
t_Items << new QTableWidgetItem(QString("%1").arg(i));
p_Items << new QTableWidgetItem(QString("55 - %1").arg(i));
s_Items << new QTableWidgetItem(QString("milliseconds: %1 ms").arg(i));
i_Items << new QTableWidgetItem(QString("Description %1").arg(i));
TableW->setItem(i-1, 0, t_Items[i-1]);
TableW->setItem(i-1, 1, p_Items[i-1]);
TableW->setItem(i-1, 3, s_Items[i-1]);
TableW->setItem(i-1, 4, i_Items[i-1]);
}

So I am trying to use a loop, I am adding some information onto the QTableWidget.
There are already 55 rows available, and I'm adding them onto the table, but for some reason, the program usually crashes.

all the t_Items and other Items are QList<QTableWidgetItem *>

guilugi
6th April 2007, 16:25
You should post a larger sample of code, a compilable one would be the best.

Also, post the debug backtrace of your crash !

VireX
6th April 2007, 17:29
I cannot debug. When I debug, there is no problem except that
setItem for column 2-4-5 don't work except for the VERY LAST row (52)

My logs say, that it stops recording in the game loop (counts to 52 and program crashes) when NOT DEBUGGING;;;; when debugging... absolutely no problems.

jacek
6th April 2007, 17:30
There are already 55 rows available, and I'm adding them onto the table, but for some reason, the program usually crashes.
Maybe there isn't enough columns?

VireX
6th April 2007, 17:42
You know what nvm, you guys don't see anything wrong with it.

Also, the fact that me commenting out CreateList(), still causes a crash (but this time moves to end of the program), but when theres CreateList() (with almost no code except a log function, which proves its something related to my program and not Qt Table).

Ok one last question
Let's say I have 2 pushbuttons:
label: Add
Labal: Del

If I were to setCellWidget the pushbutton to one column, but if I were to click the header "to sort", would it sort all the Add buttons at top and Del buttons at bottom (or vice-versa)? or would it not know how to sort PushButtons?

VireX
6th April 2007, 18:12
Is there any sample sorting code, to sort some widgets or something?