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");
}
}
Related
I generated QWidget QTGui objects with for(int i=0; i< Number;i++=) algorithm. So I can't get all Qtgui objects information When I changed something on the Widget. It gives only the last "i" value parameters. I need to get for i=0 all the parameters in one QString. After i=1 take to all parameters.
Like this:
i=0 1.Runner distance ,percentage = %80 Place = ComboBoxValue
i=1 1.Runner distance ,percentage = %60 Place = ComboBoxValue
When I changed combobox or Percentage value. Finally, I want to take the percentage and comboBox value.
Like this:
QString Runner1= QString(_Numberlabel->text() + label->text() + lineEdit->text() + comboBox->currentText());
QString Runner2= QString(_Numberlabel->text() + label->text() + lineEdit->text() + comboBox->currentText());
enter code here
for(int i=0; i < runnerList.size() ;i++)
{
Layout = new QHBoxLayout();
Layout->setSpacing(6);
_Numberlabel= new QLabel();
_Numberlabel->setObjectName(QString::fromUtf8("_Numberlabel"));
_Numberlabel->setText(QString("First Runner").arg(i+1).arg(runnerList[i][0]).arg(runnerList[i][1]));
QFont font;
font.setFamily(QString::fromUtf8("Calibri"));
font.setPointSize(10);
font.setBold(true);
font.setWeight(75);
_Numberlabel->setFont(font);
Layout->addWidget(_Numberlabel);
label = new QLabel();
label->setObjectName(QString::fromUtf8("label"));
label->setMaximumSize(QSize(60, 16777215));
label->setFont(font);
label->setText("Percantage:");
Layout->addWidget(label);
lineEdit = new QLineEdit();
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
lineEdit->setMaximumSize(QSize(50, 16777215));
lineEdit->setText("%");
Layout->addWidget(lineEdit);
comboBox = new QComboBox();
comboBox->setObjectName(QString::fromUtf8("comboBox"));
comboBox->setMinimumSize(QSize(0, 25));
comboBox->setMaximumSize(QSize(90, 16777215));
comboBox->addItem("1");
comboBox->addItem("2");
comboBox->addItem("3");
comboBox->addItem("4");
Layout->addWidget(comboBox);
this->ui.verticalLayout->addLayout(Layout);
}
You either need to keep handles to the objects you created:
// definition:
QList<QLineEdit*> lineEdits;
QList<QComboBox*> combos;
These lists should be class members.
// usage:
for(int i=0; i < runnerList.size() ;i++)
{
//...
lineEdits << lineEdit;
combos << comboBox;
}
Then, you can get the values later on:
... = QString(lineEdits[i]->text() + combos[i]->currentText());
Or you could find them by their unique object name:
ui.verticalLayout->findChild<QLineEdit*>(QString("lineEdit_%1").arg(i))->text();
// I didn't try to compile this code!
For this you need to give each QLineEdit / QComboBox a unique object name.
Or you search for all QLineEdits and hope that their order is well-defined:
ui.verticalLayout->findChildren<QLineEdit*>("lineEdit")[i]->text();
Disclaimer: This is all very ugly, but you should be able to see how it works.
Regardless of your choice, be aware:
Your line
_Numberlabel->setText(QString("First Runner").arg(i+1).arg(runnerList[i][0]).arg(runnerList[i][1]));
contains an error, because your QString(...) doesn't contain placeholders %1, %2, ..., but is followed by .arg() calls. That won't do anything except create a warning on the console.
I have a QTableView that enables MultiSelection selectionMode following a SelectRows behavior as follow:
QSqlQueryModel model = db_manager->get_all();
ui->tableView->setModel(model);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::MultiSelection);
ui->tableView->show();
The selection is working properly before which the indexes of theses selected rows are put into QModelIndexList as follow:
selectedRowsIndexesList = ui->tableView->selectionModel()->selectedRows();
Now I need to extract the information where these indexes are pointing to. I do this at the moment manually as follow:
qDebug() << model->index(0,0).data().toString();
I change the first "0" manually. But I need to automate the process through a for-loop statement like this:
for (int i = 0; i < selectedRowsIndexesList.size(); i++){
qDebug() << model->index(??,0).data().toString();
}
How can I do that ?
You already have the indexes in a list, so why go back to the model?
You can simply access the data using the indexes you've stored:
for (int i = 0; i < selectedRowsIndexesList.size(); i++){
qDebug() << selectedRowsIndexesList[i].data().toString();
}
The QModelIndexList is just a typedef as per the documentation. It is just a synonym for QList<QModelIndex> and you can iterate through the indexes as you would do through any QList variables.
for(int i = 0; i < mindex_list.size(); i++)
qDebug() << mindex_list.at(i).toString();
Or something similar.
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 generate checkboxes as follows:
foreach(QString filt, types){
QCheckBox *checkbox = new QCheckBox(filt, this);
checkbox->setChecked(true);
vbox->addWidget(checkbox);
}
I need to get access to these checkboxes by name but they are all called the same?
I need to read the text they display.
How can I go about this?
Is it possible to run a for loop and attach the value of i onto the end of the checkbox. So in effect, the checkbox would be called checkbox[0], checkbox [1], etc?
EDIT:
I've changed the code to the following:
for(int i=0; i<types.count(); ++i)
{
QString filt = types[i];
*checkboxCount = *checkboxCount + 1;
QCheckBox *typecheckbox[i] = new QCheckBox(filt, this);
typecheckbox[i]->setChecked(true);
vbox->addWidget(typecheckbox[i]);
}
I thought this was a way to dynamically name the checkboxes so I can loop through them to get the text value from them.
I'm getting the error 'variable-sized object may not be initialized' on this line QCheckBox *typecheckbox[i] = new QCheckBox(filt, this);
Any ideas to a solution/ alternate approach?
If you want to access the checkboxes later, you can just use the find children method as follows:
QStringList myStringList;
QList<QCheckBox *> list = vbox->findChildren<QCheckBox *>();
foreach (QCheckBox *checkBox, list) {
if (checkBox->isChecked())
myStringList.append(checkBox->text());
}
I am using the following code to create an object and then add a row to a qt table and populate it.
However after adding the first new row, the next time the button is clicked and a new row is added all but the first column of the row above are cleared.
What am I doing wrong?
Thanks,
First Click Adds Row Just Fine
Second Click Clears Most Of The Row Above
void MainWindow::on_btnAdd_clicked()
{
Inventory i;
int it = ui->cboItem->itemData(ui->cboItem->currentIndex()).toInt();
double q = ui->spinQnty->value();
int l = ui->cboLoc->itemData(ui->cboLoc->currentIndex()).toInt();
Item item(it);
Location loc(l);
i.insert(it,q,l);
i.setItem(item);
i.setQnty(q);
i.setLoc(loc);
QTableWidgetItem *newItem1 = new QTableWidgetItem(QString::fromStdString(i.getItem().getItem_Name()));
QTableWidgetItem *newItem2 = new QTableWidgetItem(QString::fromStdString(i.getItem().getCategory().getCatName()));
QTableWidgetItem *newItem3 = new QTableWidgetItem(QString::fromStdString(cn.dbl_to_str(i.getQnty())));
QTableWidgetItem *newItem4 = new QTableWidgetItem(QString::fromStdString(i.getLoc().getLocName()));
QTableWidgetItem *newItem5 = new QTableWidgetItem();
newItem5->setData(Qt::UserRole,QVariant(i.getInv_ID()));
QIcon qi;
qi.addFile(QString::fromStdString("red_error_warning_icon.svg"));
newItem5->setIcon(qi);
int j = ui->mainTable->rowCount();
ui->mainTable->insertRow(j);
ui->mainTable->setItem(j,0,newItem1);
ui->mainTable->setItem(j,1,newItem2);
ui->mainTable->setItem(j,2,newItem3);
ui->mainTable->setItem(j,3,newItem4);
ui->mainTable->setItem(j,4,newItem5);
}
I ended up creating a function that adds rows in order to use it elsewhere in my program.
I found that I had column sort turned on in the ui file which was affecting my item inserts.
Here is the working code for adding rows which disables sorting while it adds items.
void MainWindow::addRow(int id)
{
ui->mainTable->setSortingEnabled(false);
Inventory i(id);
QTableWidgetItem *newItem1 = new QTableWidgetItem(QString::fromStdString(i.getItem().getItem_Name()));
QTableWidgetItem *newItem2 = new QTableWidgetItem(QString::fromStdString(i.getItem().getCategory().getCatName()));
QTableWidgetItem *newItem3 = new QTableWidgetItem(QString::fromStdString(cn.dbl_to_str(i.getQnty())));
QTableWidgetItem *newItem4 = new QTableWidgetItem(QString::fromStdString(i.getLoc().getLocName()));
QTableWidgetItem *newItem5 = new QTableWidgetItem();
newItem5->setData(Qt::UserRole,QVariant(i.getInv_ID()));
QIcon qi;
qi.addFile(QString::fromStdString("red_error_warning_icon.svg"));
newItem5->setIcon(qi);
int j = ui->mainTable->rowCount();
ui->mainTable->insertRow(j);
ui->mainTable->setItem(j,0,newItem1);
ui->mainTable->setItem(j,1,newItem2);
ui->mainTable->setItem(j,2,newItem3);
ui->mainTable->setItem(j,3,newItem4);
ui->mainTable->setItem(j,4,newItem5);
ui->mainTable->setSortingEnabled(true);
}