QTableview properties - c++

I need help with customizing a QTableView, I have defined a QTableView as this example show, which I found on the internet:
model = new QStandardItemModel(2,3,this); //2 Rows and 3 Columns
model->setHorizontalHeaderItem(0, new QStandardItem(QString("ID")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Name")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Description")));
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableView->setModel(model);
How can I define a size for each column separately i.e using percentages:
I would get first column 10% of the width second 50%, third 40%.
When I run the program and double click on a row in the QTableView, I can change the value of the cell clicked on , although I have defined a QTableView onDoubleclick method , I mean its like when you click on rename a file it highlights the text so you can modify, how can I disable that?
How to make the columns resizable, meaning can be resized by dragging their columns edge.

First: Use setColumnWidth() method after setModel(). For example:
//...
ui->tableView->setModel(model);
double ii = ui->tableView->columnWidth(0);
ui->tableView->setColumnWidth(1,0.4*ii);
ui->tableView->setColumnWidth(2,0.5*ii);
Third: To do this remove
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
from your code.

Related

Add QTableView to QComboBox

I'm retrieving a set of results from the database and I want to populate the QComboBox with the resulting columns from the database (each row of the QComboBox should have the same columns as database result) and after that I would like to be able to retrieve from one row of the QComboBox a specific column and use it further in the app. I'm thinking if it would be possible to add the QTableView to QComboBox. I'm want to do this because I want to add more meaning to the results in a way that some result columns are just plain numbers and other are the description information.
I found out that it would be possible to concatenate the result and populate the QComboBox but this will leave me with only one value for each row to work with and I have to explode the string to obtain the exact part that it is needed to work with.
The popup that comes by default is a QListView, this can be changed with any object that inherits from QAbstractItemView, and in this case a QTableView will be used for it to use the setView() method, the result when clicking only should return a item of the selected row, then to set the column to be displayed after being selected will use the method setModelColumn() indicating the position of the column, but before that the model is set to the QComboBox using the method setModel().
# my model
model = new QSqlTableModel;
model->setTable("person");
model->select();
# setModel
comboBox->setModel(model);
# select column
comboBox->setModelColumn(1);
QTableView *view = new QTableView(this);
comboBox->setView(view);
Note: The model is set to QComboBox, not to QTableView. Also you could have problems with the width of QTableView, so we must resize, in my case use the following:
view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
view->setMinimumWidth(500);
The complete example can be found in the following link

Qt: avoid add text to empty cell in QTableWidget

I have QTableWidget with specific row and column. When I click on empty cell a text opens and let user to write on cell. Is there any way to avoid this situation, I mean, to avoid adding text on empty cell?
UPDATE:
I added this part to slot of cellclick(), but empty cells are yet editable,
QTableWidgetItem *item = filesTable->item(row, column);
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
Is there any way to force empty cells to be read-only?

QT check at least one row is selected QTableWidget

I need a piece of code to check that the user has selected at least one row in a QTableWidget
The QTableWidget can be referenced using ui->tableWidget.
I'm trying to check there are selecting, if not, display a message box, if so, moving on to code I have written.
Thanks.
You can obtain the selected rows from the selection model like:
QItemSelectionModel *selectionModel = ui->tableWidget->selectionModel();
QModelIndexList *selectedRows = selectionModel->selectedRows();
if (selectedRows.size() > 0) {
// There is at lease one selected row.
}

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.

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