I used QT to generate a word. I can generate a table normally, but I want to insert another table in the table, but there is a problem. The embedded table only has one row, no matter how many rows are set. The columns, however, can be set normally.
Can someone please help me see where I am going wrong,thanks.
QAxObject* WordEngine::Table_inserTable(QAxObject *table, int row, int column)
{
QAxObject *selection=table->querySubObject("Cell(int, int)", row, column);
QAxObject* range = selection->querySubObject("Range");
QAxObject* tables = m_wordDocuments->querySubObject( "Tables" );
QAxObject *pTable = tables->querySubObject("Add(QVariant,int,int)",range->asVariant(),2,3);
for(int i=1;i<=6;i++)
{
QString str = QString("Borders(-%1)").arg(i);
QAxObject *borders = pTable->querySubObject(str.toLocal8Bit().constData());
borders->dynamicCall("SetLineStyle(int)",1);
}
return pTable;
}
Related
I am working with a qtableview that is filled by a model and has two columns. I can get the contents of a selected cell no problem but only want the contents of the second column even if the first column is clicked.
void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
QString cellText;
if (index.isValid()) {
cellText = index.data().toString();
}
ui->lineEdit->setText(cellText);
}
the index looks like this for column 0:
QModelIndex(7,0,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))
And for column 1:
QModelIndex(8,1,0x55f2e5d06b00,QStandardItemModel(0x55f2e5d09740))
I tried to find a way to change the index for the cell that is clicked but I think there is no way to change it directly and I can not seem to find a way to tell my function to always use column 1 the second column.
Thanks for your time.
Code edited to reflect comment 1 below
{
QString cellText;
if (index.isValid()) {
QModelIndex idx = index;
idx = idx.sibling(idx.row(), 1);
cellText = idx.data().toString();
}
ui->lineEdit->setText(cellText);
}```
I'm trying to put a MySQL table into a treeView.
Each entry has three values in the database - id, text, parentId.
This treeView needs to be editable so I really like the QSqlTableModel approach, as the functionality to save back to database is already built in.
Now, the treeView is showing all entries in the same line, of course, and I need a hierarchy. What would be the best way to build this hierarchy, while maintaining the editing and saving capabilities?
(I'm using MySQL.)
mainwindow.h
private: QSqlTableModel* goalModel;
mainwindow.cpp
(this makes a flat table hierarchy. the table is filtered based on which entry is clicked in another table)
void MainWindow::on_tableViewGoals_clicked(const QModelIndex &index)
{
goalModel = new QSqlTableModel(this);
goalModel->setTable("goals");
//Gets the id from the clicked entry to use as filter
QString idGoals = index.sibling(row,0).data().toString();
goalModel->setFilter( "id_goals=" + idGoals );
goalModel->setEditStrategy(QSqlTableModel::OnRowChange);
goalModel->select();
ui->treeView->setModel(goalModel);
...
I tried this. It's wrong, but I don't really know why. (the third column contains the parentId value, if the entry has it)
for (int row = 0; row < goalModel->rowCount(); ++row)
{
//if the entry has a value over 0 in the parentId column
if (goalModel->index(row,2).data().toInt() > 0)
{
//get number in column 2
int parentId;
parentId = goalModel->index(row,2).data().toInt();
QModelIndex newChild = goalModel->index(row,0);
//find QModelIndex row with matching number in column 0
for (int row = 0; row < goalModel->rowCount(); ++row)
{
if (goalModel->index(row,0).data().toInt() == parentId )
{
//make this entry that entry's child
QModelIndex newParent = goalModel->index(row,0);
newChild = goalModel->index(0,0,newParent);
}
}
}
}
All indexes are already made, so no need to make new ones, just assign a parent to all those who have one. How best to do that?
I have the data in Qtableview, where my task is, when ever I do multiple selection of rows I want the content of all selected rows. or at least I would like to know what are the row numbers I have selected.
For this I have written code which is giving 'Zero' every time even though I don't select any row.
ui->xvalue->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->xvalue->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->xvalue->show();
QModelIndexList indexList = ui->xvalue->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
row = index.row();
}
printf("\n%d\n",row);
I even tried this but no use I am not getting the answer
QItemSelectionModel* selectionModel = this-> ui->xvalue->selectionModel();
QModelIndexList selected = selectionModel->selectedIndexes();
int num;
for(int i= 0; i< selected.count();i++)
{
QModelIndex index = selected.at(i);
qDebug() << index.row();
num = index.row();
printf("selected row is %d", num);
}
In my Qt application, I have a window with a table which gets dynamically updated with data from a backend server. I need my window to be able to open an excel instance, insert all the data in the table to excel window and update the excel cells when data in my table gets updated.
Is this something that is possible to achieve? If so, how I can get this done? I do not mind a platform dependent solution which can only run on Windows.
This is the code I used finally.
Opening Excel:
QAxObject* excel = new QAxObject("Excel.Application", 0);
excel->dynamicCall("SetVisible(bool)", true);
Reading cell values:
QAxObject* workbooks = excel->querySubObject("Workbooks");
QAxObject* workbook = workbooks->querySubObject("Open(const QString&)", "D:\\a.xlsx");
QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1);
QAxObject* usedrange = worksheet->querySubObject("UsedRange");
QAxObject* rows = usedrange->querySubObject("Rows");
QAxObject* columns = usedrange->querySubObject("Columns");
int intRowStart = usedrange->property("Row").toInt();
int intColStart = usedrange->property("Column").toInt();
int intCols = columns->property("Count").toInt();
int intRows = rows->property("Count").toInt();
QAxObject* cell;
for (int i = intRowStart; i < intRowStart + intRows; i++)
{
for (int j = intColStart; j < intColStart + intCols; j++)
{
cell = excel->querySubObject("Cells(Int, Int)", i, j);
QVariant cellValue = cell->dynamicCall("value");
qDebug() << cellValue.toString();
}
}
Writing cell values
cell->setProperty("Value", "somevalue");
Note: Remember to create QApplication before doing any of these. I spent a considerable amount of time figuring out what is wrong by not doing this.
I have a QTableWidget, which displays files.
What I want to do is be able to select 1 or multiple rows from this table and pass the first column contents of each row into a function to be able to manipulate.
QModelIndexList indexList = ui->filesTable->selectionModel()->selectedIndexes();
int row;
foreach (QModelIndex index, indexList) {
row = index.row();
qDebug() << row;
}
I've got this code but this passes the indexes in and I need the contents of the first column of the QTableWidget on the row or rows I select.
Thanks for any help in advance!
To get the content of a cell you need to use QModelIndex::data method:
QModelIndexList indexList = ui->filesTable->selectionModel()->selectedIndexes( );
foreach (QModelIndex index, indexList)
{
qDebug() << index->data( Qt::DisplayRole );
}
You can retrieve more information about selected cells just changing the role. Custom models can accept custom roles.