QT QTableWidget not appearing in QTabWidget - c++

I have an application which uses multiple tabs. I used QTabWidget. On some tabs I needed to show tables, so I used QTableWidget.
The code snippet is:
QWidget *qwgt = qPreviewTabs->widget(Index);
QTableWidget *qDrvTab = new QTableWidget();
....
....
....
QVBoxLayout *vbLyt = new QVBoxLayout();
vbLyt->addWidget(qDrvTab);
qwgt->setLayout(vbLyt);
When I add push buttons and tree widgets they all appear on the specified tab without any problem. Only the QTableWidget refuses to show.

A table with no rows and columns is a void.
So do
qDrvTab->setRowCount(no_of_rows);
qDrvTab->setColumnCount(no_of_cols);
before adding it to layout.
Now you can see your Tablewidget in layout.

Related

Add QIcon to QTableView cell

I have already coded this for QTableWidget:
void ReadOnlyWindow::addReportIconToRow(const int rowIndex){
QIcon icon;
QSize sz(16, 16);
icon.addPixmap(style()->standardIcon(QStyle::SP_FileDialogEnd).pixmap(sz), QIcon::Normal);
QTableWidgetItem *iconItem = new QTableWidgetItem();
iconItem->setText("report");
iconItem->setIcon(icon);
iconItem->setFlags(iconItem->flags() & (~Qt::ItemIsEditable));
ui->homeWorksTable->setItem(rowIndex, REPORT_COLUMN_INDEX, icon);
}
REPORT_COLUMN_INDEX is const int from class and it has value 4.
I am trying to found out how to rewrite code if table is `` QTableView`.
I was trying to use setItemData() and setData() but I think I used it in bad way because it didn't work.
P.S.: Now I want to do it for QTableView because its easy to load SQLite table there. This part works. I also added one more column. Now I need add to all rows of this column icon with text (how is in my code for QTableWidget). Function up there should be for one cell and will be implemented in loop.
to add an icon to a tableWidget you set the item like specified here :
and when defining the QTableWidgetItem, use the constructor taking an Icon to be displayed.
here is a short example:
this->ui->myTable->setItem(row, col, new QTableWidgetItem(QIcon(":/resources_to_icon_.png"),"SomeText"));
in your code:
ui->homeWorksTable->setItem(rowIndex, REPORT_COLUMN_INDEX, QTableWidgetItem(icon,"some text");

QTableWidget, centering cellWidgets

Is there a way to place QCheckBox as a cell widget of QTableWidget in the center of a cell, not at the left side, without additional QWidget and adding the checkbox to it's layout?
Use setCellWidget to add QCheckBox to table:
QWidget *checkBoxWidget = new QWidget(); //create QWidget
QCheckBox *checkBox = new QCheckBox(); //create QCheckBox
QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); //create QHBoxLayout
layoutCheckBox->addWidget(checkBox); //add QCheckBox to layout
layoutCheckBox->setAlignment(Qt::AlignCenter); //set Alignment layout
layoutCheckBox->setContentsMargins(0,0,0,0);
ui->tableWidget->setCellWidget(0,0, checkBoxWidget);
Also use these line to resize to contents:
ui->tableWidget->resizeRowsToContents();
ui->tableWidget->resizeColumnsToContents();
setCellWidget: Sets the given widget to be displayed in the cell
in the given row and column, passing the ownership of the widget to
the table.
Reference: https://evileg.com/en/post/79/
I have a simple solution without any additional layout if you want to use ui->tableWidget->resizeColumnsToContents():
Use checkBox->setStyleSheet( "text-align: center; margin-left:50%; margin-right:50%;" );
AFTER this, call ui->tableWidget->resizeColumnsToContents().
If the column is resized after this (eg. the table gets resized by the layout), you may have to call ui->tableWidget->resizeColumnsToContents() again.
It the widget is placed before the resulting column width is known, it retains its (wrong) position. Using resizeColumnsToContents() repositions it.

How to add body to QTabWidget

i'm stuck ...
I create QTabWidget
h_mainTabAccounts = new QTabWidget(ui.centralWidget);
h_mainTabAccounts->setObjectName(QStringLiteral("h_mainTabAccounts"));
h_mainTabAccounts->setGeometry(QRect(0, 0, 1281, 781));
h_mainTabAccounts->setElideMode(Qt::ElideMiddle);
h_mainTabAccounts->setMovable(false);
int index = h_mainTabAccounts->addTab("first",NULL);// QString::fromStdString(a.user));
And now, i want to add a table to this TABS (i have 3-4 TABS) , and want to add other tables to tab.
How to add widget (table - QTableWidget) , and how to fit my table to TAB BODY (of QTabWidget)
And now, i want to add a table to this TABS (i have 3-4 TABS) , and
want to add other tables to tab.
So to have the QTableWidget be the content of your first tab, you'd change this:
int index = h_mainTabAccounts->addTab("first",NULL);// QString::fromStdString(a.user));
to this:
QTableWidget * myTableWidget = new QTableWidget;
int index = h_mainTabAccounts->addTab(myTableWidget, "first");
... and so on for whatever other tabs you wanted to add.
As far as fitting the table size to the size of your QTabWidget, that should happen automatically.

QT 5.3 QListView column problems

guys! I'm newbie in QT Programming.
I want to ask, why columns is appearing as usual row on my QListView control?
QStandardItemModel* model = new QStandardItemModel(0, 1, ui->listView);
QList<QStandardItem*> items;
items.append(new QStandardItem("Column 1"));
model->insertColumn(0, items);
ui->listView->setModel(model);
QListView simply can not display multiple columns. Use QTableView.

assigning model to tableview and creating rows and columns

I am New to Qt creator .
I want to create a table View with 1 row and 2 columns using QmodelIndex.
There was an error in assigning the model to the tableView and creating data .
QTableView* const tableView
= { htca_ui->tableView,
};
tableView->setModel(&mymodel);
How to create a model and assign to the view to create the row with 2 columns using QmodelIndex?
Please Help
If you have added a tableView with the UI designer you don't need to create it again in code. Assuming your QTableView is called tableView and that htca_ui is your UI pointer, you can do
htca_ui->tableView->setModel(&mymodel);
Edit : And it occurs to me the &mymodel means you're creating it on the stack, which may not be the best idea. Better to do :
MyModel *mymodelinstance = new MyModel(this);
htca_ui->tableView->setModel(mymodelinstance);
However: the model classes can be complex to work with. If you are just after a simple table with minimum code, I would suggest using a QTableWidget rather than a QTableView. Add a QTableWidget to your UI, then you can do
htca_ui->tableWidget->setRowCount(1);
htca_ui->tableWidget->setColumnCount(2);
QTableWidgetItem *item = new QTableWidgetItem("An Item");
htca_ui->tableWidget->setItem(0,0, item); // The item at the top left will read "An Item".
See the documentation for QTableWidget for more info