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.
Related
I want to create an editable QComboBox which filters results according to the search query and updates the dropdown entries accordingly.
After reading How do I Filter the PyQt QCombobox Items based on the text input? I tried to implement something similar in C++.
But I can't store anything inside the QComboBox now. Even after adding new entries through addItem() the total count remains 0.
What is the reason for this and how do I insert entries inside the QComboBox with QSortFilterProxyModel?
Here is the relevant snippet of the code:
SearchBox = new QComboBox(this);
SearchBox->setEditable(true);
// Try adding a few entries and check if they persist after changing the model
SearchBox->addItem(QString("hi"));
SearchBox->addItem(QString("bye"));
int count = SearchBox->count(); // count = 2
ProxyModel = new QSortFilterProxyModel;
ProxyModel->setSourceModel(SearchBox->model());
ProxyModel->setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
SearchBox->setModel(ProxyModel);
// Check count again
count = SearchBox->count(); // count = 0 <- Why?
// Try adding new entries
SearchBox->addItem(QString("Hi again"));
count = SearchBox->count(); // count = 0 .. So new entries don't get stored
Completer = new QCompleter(ProxyModel,this);
Completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
SearchBox->setCompleter(Completer);
QObject::connect(SearchBox->lineEdit(), SIGNAL(textChanged(const QString)), ProxyModel, SLOT(setFilterFixedString(const QString)));
QObject::connect(Completer, SIGNAL(activated(const QString &)), this, SLOT(onCompleterActivated(const QString &)));
Use QStringListModel to store items. Application crashes if proxy model have no items (if filter string filters out all items)(this needs further investigation - is this completer issue or combobox). This can be fixed by not applying such filter (onTextChanged(QString text) slot). Completer completes input if theres only one item (not sure if it's ok). And sometimes checkbox doubles all items (don't know why). If this issues is critical, I think you need to write custom ComboBox from scratch and this is serious work.
{
SearchBox = new QComboBox(this);
SearchBox->setEditable(true);
QStringList Items;
Items << "hi" << "bye";
StringListModel = new QStringListModel();
StringListModel->setStringList(Items);
ProxyModel = new QSortFilterProxyModel;
ProxyModel->setSourceModel(StringListModel);
ProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
SearchBox->setModel(ProxyModel);
// Check count again
int count = SearchBox->count(); // count = 2
// Try adding new entries
QStringList Items_ = StringListModel->stringList();
Items_ << "hi again";
StringListModel->setStringList(Items_);
count = SearchBox->count(); // count = 3
Completer = new QCompleter(ProxyModel,this);
Completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
SearchBox->setCompleter(Completer);
QObject::connect(SearchBox->lineEdit(), SIGNAL(textChanged(const QString)), this, SLOT(onTextChanged(QString)));
QObject::connect(Completer, SIGNAL(activated(const QString &)), this, SLOT(onCompleterActivated(const QString &)));
}
void MainWindow::onTextChanged(QString Text) {
QStringList Items = StringListModel->stringList();
QString Item;
foreach(Item,Items) {
if (Item.indexOf(Text) > -1) {
ProxyModel->setFilterFixedString(Text);
return;
}
}
}
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 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 have a table and each row in the table has a checkbox in it's first column. I need to make it so I can detect which checkboxes are checked and delete those rows when a button is pressed.
QWidget * chkWidget = new QWidget();
QHBoxLayout *center = new QHBoxLayout();
center->setAlignment( Qt::AlignCenter );
center->addWidget( new QCheckBox );
chkWidget->setLayout( center );
ui->data_table->setCellWidget(rowCount,0, chkWidget);
Was this done right? If so how do I access the checkboxes at each row?
I talk about a QTableWidget. You can use a QList.You save your QCheckBox into this QList and use it, when there is some change
Maybe you should check out the documentation
QTableWidget: Link
QList: Link
Here is a solution. I cannot run it at the moment, so please tell me if it works. Please validate the row value. I am not sure if it's possible, that row can have the value -1 when you delete the last row ;)
#include "TestTableWidget.h"
#include "ui_TestTableWidget.h"
TestTableWidget::TestTableWidget(QWidget *parent) : QMainWindow(parent), ui(new Ui::TestTableWidget)
{
ui->setupUi(this);
tableWidget = new QTableWidget(this);
tableWidget->setColumnCount(1); // Just an example
ui->gridLayout->addWidget(tableWidget);
connect(tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(slotChange()));
for(int i = 1; i < 10; i++)
{
addRow("Row " + QString::number(i));
}
}
TestTableWidget::~TestTableWidget()
{
delete ui;
}
void TestTableWidget::addRow(QString text)
{
int row = tableWidget->rowCount();
qDebug() << "Current row count is " + QString::number(row);
// Add new one
QTableWidgetItem *item = new QTableWidgetItem(text);
tableWidget->insertRow(row);
tableWidget->setItem(row, 0, item);
// Add item to our list
}
void TestTableWidget::slotChange()
{
int row = tableWidget->currentRow();
qDebug() << "Change in table. Current row-index: " + QString::number(row);
// This value is zero-based, so you can use it in our list
}
I am just trying to add widgets into my table widget and I am trying the code below but all the time I run the program, the first widget is added but the rest is not added. Can you please help me for this situation ?
if(req.at(index).request.CodedValue.size() > 1 )
{
int rowNumber = -1;
for ( int paramNumber = 0 ; paramNumber < req.at(index).request.params.size(); paramNumber++)
{
if(req[index].request.params[paramNumber].semantic == "DATA")
{
rowNumber++;
QComboBox* reqComboBox = new QComboBox();
QLineEdit* tableReqLineEdit = new QLineEdit();
for ( int codedCounter = 0; codedCounter < req.at(index).request.CodedValue.at(paramNumber).trams.size(); codedCounter++)
{
// you should look for the subfunctions and add according to them
reqComboBox->addItem((req[index].request.CodedValue[paramNumber].trams[codedCounter].valueName));
QObject::connect(reqComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(on_tableCombobox_currentIndex());
}
ui.tableWidget->setCellWidget(rowNumber,1,reqComboBox);
}
}
}
Use qDebug in order to see how many times the for loop is executed. Probably it is executed only once:
#include <QDebug>
...
rowNumber++;
qDebug() << rowNumber;
...
Try the following:
for (int i=0; i<ui.tableWidget->rowCount(); i++)
{
ui.tableWidget->setCellWidget(i,1,new QLineEdit);
}
How many line edits do you see?
Notice that you should use the setRowCount in order to set the number of rows of your table widget.