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.
Related
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;
}
void IdListForTrain::SetListIdInList(QStringList IdList)
{
QTableWidgetItem *item=nullptr;
for(int index=0;index<IdList.count();index++)
{
ui->Id_tableWidget->insertRow(index);
for(qint32 columnIndex=0;columnIndex<=1;columnIndex++)
{
item = new QTableWidgetItem;
if(columnIndex==0)
{
ui->Id_tableWidget->setItem(index,columnIndex,item);
item->setText("00:00:00");
}
if(columnIndex==1)
{
ui->Id_tableWidget->setItem(index,columnIndex,item);
item->setText(IdList.at(index));
}
QString tmp1 = ui->Id_tableWidget->item(index,1)->text();
QString tmp = item->text();
}
}
}
Hi! I am new to Qt and I'm facing a problem. I was asked to create a tablewidget with the effective date of train Ids in one column and the names of the trains in another column,I did this code to the best of my knowledge but I can't set or access the data in the widget in the line eventhough the code doesnt give any errors but it crashes everytime I open the application window at this line,
QString tmp1 = ui->Id_tableWidget->item(index,1)->text();
I'm not sure what the reason is.
Let look at first iteration - index=0 and columnIndex=0.
You go through if(columnIndex==0) condition and set item to
QTableWidget's row = 0 (index) and column = 0 (columnIndex)
Second condition skipped
You try to access item at row = 0 (index) and column = 1 with code QString tmp1 = ui->Id_tableWidget->item(index,1)->text();. But at this time you have no item assigned to that column
That's all!
my problem is not so simple for me, because I'm a niewbie in qt, so asking to the experts: the problem, I populate dynamically a QTableWidget from a button on a toolbar, selecting data from a *.csv file.
All works fine but the formatting of the last column text rests on center-vertical position.
I had tryed to change styles or set directly on the code result: the app crash or nothing appens to the last column text.
(And if is possible how to change headers appearance).
Thank You in advance (& sorry for English mistakes).
void ToolBar::setDrawTable(QStringList sl)
{
m_table->clear();
QStringList slHeader = sl.at(0).split(';');
sl.removeAt(0);
m_table->setRowCount(sl.count());
m_table->setColumnCount(slHeader.count());
m_table->setHorizontalHeaderLabels(slHeader);
for (int r= 0; r<sl.count(); r++){
QStringList slRow = sl.at(r).split(';');
for (int c = 0; c < slRow.count(); c++)
{
QTableWidgetItem *item = new QTableWidgetItem(slRow.at(c));
m_table->setItem(r, c, item);
item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}
slRow.clear();
}
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
I am trying to create a QTextTable and insert data into it. Currently I am unable to create the table because of several errors
use of undeclared identifier 'editor'
I am also unsure how I can insert data into the TextTable. My code is below
QTextCursor cursor(editor->textCursor());
cursor.movePosition(QTextCursor::Start);
QTextTable *table = cursor.insertTable(5, 3);
I tried the code below and I have no error I am just wondering how i can insert data into the texttable so i can print it?
QTextEdit *editor = new QTextEdit();
QTextCursor cursor(editor->textCursor());
cursor.movePosition(QTextCursor::Start);
QTextTable *table = cursor.insertTable(5, 3);
table->insertRows(0, 5);
Add a text browser and try this one. (The form contains a QTextBrowser with object name textBrowser)
QTextCursor cursor(ui->textBrowser->textCursor());
cursor.movePosition(QTextCursor::Start);
QTextTable *table = cursor.insertTable(2, 3);
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
table->cellAt(i, j).firstCursorPosition().insertText("Hello");
}
}
I wrote a gui program which will connect to an oracle db an retrieve data after a query is typed. the retrieved data is shown in a QTableView table model widget. and later the result of the QTableView is exported to a .csv file
QString MyQuery = ui->lineQuery->text();
db.open();
QSqlQuery query(MyQuery,db);
if(query.exec())
{
qDebug()<<QDateTime::currentDateTime()<<"QUERY SUCCESS ";
ui->queryButton->setStyleSheet("QPushButton {background-color: rgb(0, 255, 0);}");
this->model=new QSqlQueryModel();
model->setQuery(MyQuery);
ui->tableViewOra->setModel(model);
QString textData;
int rows=model->rowCount();
int columns=model->columnCount();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
textData += model->data(model->index(i,j)).toString();
textData += ", "; // for .csv file format
}
textData += "\n"; // (optional: for new line segmentation)
}
QFile csvfile("/home/aj/ora_exported.csv");
if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate))
{
QTextStream out(&csvfile);
out<<textData;
}
csvfile.close();
}
now the problem is, during a query I observed there are 543 rows visible in the QTableView (which is correct cause there are 543 entries). But when the .csv file is exported, only 256 rows are there.
Is there any variable size constraint that I am unaware about ???
Which variables should be taken care of if I want to export .csv files of upto 1000 rows approx ??? thanks.
I think when you first read model->rowCount() the model has not fetched all the results completely. Although it will fetch more later when table view is displayed resulting in a full display of rows in the table view.
Try to use QSqlQueryModel::fetchMore before reading the row count :
while (model->canFetchMore())
model->fetchMore();
int rows=model->rowCount();
int columns=model->columnCount();
[Additional information from Tay2510]:
You could just change the file open flag
if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate))
to
if(csvfile.open(QIODevice::WriteOnly))
The former will overwrite the same file while the latter append data on it.