Normal QTablewidget is as follows:
a b
1 data data
2 data data
3 data data
I want to remove the first column that shows row numbers.
My table should look like this:
a b
data data
data data
data data
I can not find the way to do this. Let me know if there is a way to add items more easily.
That 's how I implemented my table:
QString a[5];
a[0]="ddd";
QTableWidgetItem *item1 = new QTableWidgetItem(a[0]);
ui->tableWidget->setItem(0,0,item1 );
There are two views in QTable the vertical and the horizontal header, they are defined in QTableView, as any widget you can hide them so it can be done as below :
ui->tableWidget->verticalHeader()->setVisible(false); // Get Vertical header and hide it
You can have vertical header in easy way as one line code,
ui->tableWidget->setHorizontalHeaderLabels(QString("HEADER 1;HEADER 2;HEADER 3;HEADER 4").split(";"));
For setting the data in Table you can use QString Array which contains data using for loop you can store the data in table, but you need to take care about indexing.
Related
In Qt model-view how is setData different than insertRows. I understand that insertRows is just for new rows addition, whereas setData can modify existing data as well. Can setData also be used to insert additional rows in a table or list based model. I am new to Qt, so please pardon if the question is too basic.
No, setData() is to be used to change existing data, or more precisely to manipulate data for an index that exists. You cannot add rows using this method.
setData puts new data into your buffer, insertRows will add new rows into the indexed location of your buffer, increasing the total amount of data you now have. If you are trying to only overwrite data at certain locations I suggest using replace.
I have QTableView and QAbstractTableModel. I require rows to have height equal to 24. I know the only way to do this is by calling QTableView::setRowHeight. Since the model is dynamic it may be added new rows, but I don't want to call setRowHeight each time new row is added.
How can I configure QTableView such that it uses the same height for new added rows or can a model be sent the height of rows?
For Qt versions < 5
QHeaderView *verticalHeader = myTableView->verticalHeader();
verticalHeader->setResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(24);
For Qt versions >= 5 use
QHeaderView *verticalHeader = myTableView->verticalHeader();
verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(24);
If that function doesn't apply to vertical headers, you likely will have to call setRowHeight() every time you add a new row.
Aloha
I have a QTableWidget with two columns that are currently using a ComboboxDelegate (my subclass of QItemDelegate) to present options to the user. I'd like the choice in the first column to effect the options available in the second, for the current row only.
E.g have a list of cars in the first column, and in the second a list of colours which are available for that car. Other rows to have different cars selected and thus different colour choices available.
From what I can see, I can only set an item delegate per row or column, so I can't see how to change the options in the second column's delegate without affecting all the other rows.
Is this possible? I'd really like to avoid going to a full view/model separation as I have quite a bit of code looking at this QTableWidget already (and I'm under time pressure)
Well for those interested; I went back to my pre-delegate approach, which was to use QTableWidget::setItemWidget() to provide a combobox widget for each cell.
I subclassed qcombobox to take a reference to the table, and connected the combobox CurrentIndexChanged with a slot to update the table data.
(setting a widget in a cell does not affect the tablewidget data unless you do this).
Using a full combobox like this is more expensive than an itemdelegate, but my tables are very small so I can get away with it. The rendering of the combobox is not as nice as the delegate (the combobox is visible all the time instead of only during editing in the delegate's case), but with time I'm sure I can improve on this.
Right now I have where the tablewidget and calendar resizes as the window is expanded/contracted. But as you can see my table (2 columns, x rows) also follow the layout. How do I break out of this? What I want is the calendar and table widget to resize as the form expands and the two columns filling up the entire widget. Something like this:
You have to set size policies for these widgets with setSizePolicy() method - here is docs . And chose behavior you desire from this enum
For example, if you want tablewidget to take all available space:
tablewidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
Is there a way to re-implement the sorting function for QTableWidget? I use QComboBox (multicheck) in the first row of my table to filter the data. I also want be able to sort the data in the table, but I want my widget to stay in the first row no matter how I sort the data.
I know that I can re-implement the < operator and set a value as well as a widget in a cell, but that does of course not help me.