I'm using QTableView with QSqlTableModel. In my view I don't display the column containing record id. How can I acquire id of the selected row if its not displayed in any column ?
Thanks for help :)
Also you can retrieve id directly from QSqlQueryModel but I am not sure it's more convinient then proposed by soulSurfer.
Using QModelIndex for desired row:
QSqlQueryModel *model = tableView->model();
QSqlRecord record= model->record(desiredIndex->row());
QSqlField field = record.field(id_column_index);
int id = field.value().toInt();
Hmmm...one way is to get the ID from the model and hide it in the view with
void QTableView::setColumnHidden (int column, bool hide)
then you basically get it, but hide it, and from here, you can get it easily with from the model directly by using the index emited from
void QAbstractItemView::activated ( const QModelIndex & index )
Signal.
Related
Im really a noob at QT so go easy on me
So i have 2 QListviews
1. TimeList
2. DateList
What im trying to Do is that when i select an item in Timelist the same index in Datelist will be selected and after that i will send the data to a datetime edit
I tried to use that QAbstractItemmodel model->index(row,column);
and createindex but i dont know syntax that well so i messed it up
QModelIndex i = ui->TimeList->currentIndex(); //it gets the index when an item is clicked
ui->DateList->setcurrentIndex(i); //it sets the same index in the other QListView
ui->DateList->clearSelection();
ui->DateList->selectionModel()->select(i, QItemSelectionModel::Select); //This highlights the same index in other QListView and it works fine
QTime t = i.data(Qt::DisplayRole).toTime(); // It converts to QTime fine
i = ui->DateList->currentindex(); // i try to change the value of index to get the date
QDate d = i.data(Qt::DisplayRole).toDate();
ui->TimeEdit->setTime(t);
ui->DateEdit->setDate(d);
What i have gathered is that i.data output is invalid for QDate because it says so in qDebug so.
So i suppose index doesnt only hold row and column values but how to assign it to Datelist is beyond me. (:
Thanks in advance
I just needed to learn syntax to create a new QIndexModel
QModelIndex i = ui->TimeList->currentIndex();
QModelIndex i2 = ui->DateList->model()->index(i.row(), 0);
So i use the same row value from index1 but its assigned to the second QListview
I have a SQL model that connects to a single table, this table will change the number of columns depending on certain conditions during the execution of the program. The model is connected to a QTableView.
I have a function that controls the number of columns at the end of the function i have a call to model->select(), to update the information of the model and tableView->reset(), to what i thought would rearrange the view adding or taking away columns.
The problem is that the view does not change from the original number of columns that it had. If i reduce the number i can see that the data change and show empty on the missing columns. Is there a command for the tableView to resize it self?
Editing the question
in the constructor of the class i'm reading the table and setting it to the view:
header = new QSqlTableModel(parent,data->m_db);
header->setTable("C"+QString::number(markTime.toSecsSinceEpoch())+"T");
header->select();
ui->heading->setModel(header);
ui->heading->show();
Every time that that the number of columns is changed is an SQL procedure to change the number of columns:
void ImportProcess::copyTable(QString oldTable, QString newTable)
{
QSqlQuery queryOld, queryNew;
queryOld.prepare("select * from :oldTable");
queryOld.bindValue(":oldTable",oldTable);
queryOld.exec();
if(queryOld.record().isEmpty()==true) return; //Old table was empty, nothing to copy
int oldColumn=queryOld.record().count();
QString replaceLine="insert into "+newTable+" values(";
while(queryOld.next()==true)
{
replaceLine.append(QString::number(queryOld.value(0).toInt()));
replaceLine.append(", "+queryOld.value(1).toString());
for(int y=0;y<(oldColumn < ui->columns->value() ? oldColumn : ui->columns->value());y++)
{
replaceLine.append(", "+QString::number(queryOld.value(y+2).toFloat()));
}
replaceLine.append(")");
queryNew.exec(replaceLine);
}
}
Then the header file is updated, and here is where I thought that the tableview will be redrawn:
void ImportProcess::updateHeadingTable()
{
QSqlQuery query;
query.exec("delete from C"+QString::number(markTime.toSecsSinceEpoch())+"T");
QString description= ui->Week->isChecked() == true ? "Week" : "Size";
query.exec("insert into C"+QString::number(markTime.toSecsSinceEpoch())+"T (id, description) values (101, '"+description+"')");
for(int x=0;x<ui->columns->value();x++)
{
query.exec("update C"+QString::number(markTime.toSecsSinceEpoch())+"T set col"+QString::number(x)+" = '30'");
}
header->select();
ui->heading->reset();
}
I believe you forgot about some protected methods:
void QAbstractItemModel::beginInsertColumns(const QModelIndex &parent, int first, int last);
void QAbstractItemModel::beginRemoveColumns(const QModelIndex &parent, int first, int last);
void QAbstractItemModel::endInsertColumns();
void QAbstractItemModel::endRemoveColumns();
Every time the number of column about to be change you should call first or second method. After change you should call third or fours method.
You can read about these methods in Qt documentation.
I am new with QT, so I'll apprciate any help.
In my application, I'm creating a QStandardItemModel with rows and columns. Now I want to save in one cell a list of QStrings - but I dont know how to do that.
I've tried to write this code:
QStandardItem* dataRecords = new QStandardItem();
QList<QStandardItem* > list;
QList<QString>::const_iterator dataRecord;
for( dataRecord = i.value()->begin(); dataRecord != i.value()->end(); ++dataRecord )
list << new QStandardItem((*dataRecord));
dataRecords->appendRows(list);
model->setItem(row, 3, dataRecords);
i is a QList of QString.
Now, I dont know how can I access abd retrive the QString values from the model.
Can anyone please help me? or suggest me another way to do that?
Thanks!
You should read some docks about Model\View proggramming in Qt
To access data stored in model you should use: QVariant QStandardItemModel::data ( const QModelIndex & index, int role = Qt::DisplayRole )
To get QModelIndex for particular cell use: QModelIndex QStandardItemModel::index ( int row, int column, const QModelIndex & parent = QModelIndex() )
Some code example...
QModelIndex superIndex = model->index(i,j);
QString superData= model->data(superIndex).toString();
I’m new to Qt, I need help with getting the value of a combobox in a table widget.
I use “setCellWidget” to add a combobox(in my case, its name is “settingA”) to a table widget (the name is “tableWidget_4”):
QComboBox* settingA = new QComboBox();
settingA->addItem("100");
settingA->addItem("200");
ui->tableWidget_4->setColumnCount(1);
ui->tableWidget_4->setRowCount(3);
ui->tableWidget_4->setCellWidget ( 0, 0, settingA );
What I want to do here is:
When a button (its name is “ApplyComboButton” in my case) is clicked, I want the value of the combobox(settingA) can be saved into a QStringList(InputComboData) , and this is how I try to do this:
void MainWindow::on_ApplyComboButton_clicked()
{
QStringList InputComboData;
InputComboData << ui->tableWidget_4->item(0,0)->text();
}
And it fails.
How can I get the value of my combobox?
You can use the QTableWidget::cellWidget ( int row, int column ) function to get your QComboBox widget. Use qobject_cast to cast it to QComboBox, and use the currentText() function to get the text.
QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget_4->cellWidget(0,0));
InputComboData << myCB->currentText();
Use QTableWidget's cellWidget to get a QWidget* to the widget you set as cellWidget (don't forget to use qobject_cast or dynamic_cast to cast that pointer to a QCombobox*)
I'm trying to get text and data from column in index number 0 from row that is selected
but I never get the right data I'm using simple model view TreeView with QSortFilterProxyModel proxy to sort the columns and QStandardItemModel as the model
This is the slot function that is triggered on each doubleClicked
connect(ui.treeView_mainwindow, SIGNAL(doubleClicked( const QModelIndex &)), this,SLOT(tree_itemClicked( const QModelIndex &)));
....
...
void MainWindowContainer::tree_itemClicked(const QModelIndex & index)
{
int iSelectedRow = index.row();
QString groupID;
QString groupName;
groupID = m_model->item(iSelectedRow,0)->data(Qt::UserRole).toString();
groupName = m_model->item(iSelectedRow,0)->text();
}
UPDATE:
Well, I found the answer but I have another question, the answer is :
QString groupID = index.model()->index(index.row(), 0, index.parent()).data(Qt::UserRole).toString();
QString groupName = index.model()->index(index.row(), 0, index.parent()).data(Qt::DataRole).toString();
}
My other question is how do I set data to column in index ( for example: 3 ) in the selected row?
The problem here most likely is that index.row() points to the row in the proxy model after sorting. This is most likely not the same row in your unsorted source model.
Try the following instead:
groupID = m_proxy_model->index(iSelectedRow,0).data(Qt::UserRole).toString();
Have you tried using
QStandardItem * QStandardItemModel::itemFromIndex ( const QModelIndex & index ) const;
Perhaps your rows are not set properly.
If this does not help, you should give an example tree, indicate what you click, what you expect, what you get.
QModelIndex modelIndex = m_proxy_model->index(iSelectedRow,0);
m_proxy_model->data (modelIndex ,Qt::UserRole).toString();