How to get value from QComboBox
I am having trouble getting the index value from a QComboBox that uses a model loaded with Data from a table in a database.
I can get the value out in the same function that loads the QComboBox but not from a button clicked function.
I am sure there is something simple, but it eludes me.
Here is the code that works
QString qlstr = "SELECT * from CellList ORDER BY designator ASC";
QSqlQuery modelquery;
modelquery.exec(qlstr);
QSqlQueryModel *modelCell = new QSqlQueryModel(this);
modelCell->setQuery(modelquery);
modelCell->setHeaderData(0, Qt::Horizontal, "UserID");
ui->cmbCellList->setModel(modelCell);
ui->cmbCellList->setModelColumn(1);
QSqlRecord record = modelCell->record(ui->cmbCellList->currentIndex());
QString lstr;
lstr = record.value(0).toString();
ui->lineEdit->setText(lstr);
but in the button click function when i try to fill the lineEdit with
QSqlRecord record = modelCell->record(ui->cmbCellList->currentIndex());
QString lstr;
lstr = record.value(0).toString();
ui->lineEdit->setText(lstr);
program breaks. It seems as if the modelCell is no longer available.
Any help would be most appreciated.
Re: How to get value from QComboBox
You have a modelCell pointer declared locally to your first bit of code, presumably in the scope of the constructor. If that is the case, then the second bit of code accesses a modelCell pointer than cannot be the same variable: probably declared as a class member but never initialised.
Re: How to get value from QComboBox
All this code is in MainWindow class. In the header I define modelCell as
Then I create the pointer. I thought this would allow the pointer to be used in any of the functions of the mainwindow class.
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
Here is the function I am trying to access the pointer. What am I not doing correctly?
I must be missing something in my fundamental knowledge.
Code:
void MainWindow::on_btnSaveConductivityTest_clicked()
{
QSqlRecord record
= modelCell
->record
(ui
->cmbCellList
->currentIndex
());
lstr = record.value(0).toString();
ui->lineEdit->setText(lstr);
Re: How to get value from QComboBox
May Be data is there at
Code:
record.value(0).toString()
or
Check
Code:
ui->cmbCellList->currentIndex()
Re: How to get value from QComboBox
basha, it can find the values while in the main function but in the button function it blows up. It can not find any records outside the main function even though I made the model public. There is something basic I am not understanding I'm sure.
Re: How to get value from QComboBox
ChrisW67 gave you the answer in post #2:
Quote:
. . . probably declared as a class member but never initialised.
You declare modelCell as a unintialized member variable in your header file. Then in the constructor you re-declare and initialize modelCell as a local variable which goes out of scope at the end of the ctor block.
Google "variable shadowing".
In the constructor use this:
Re: How to get value from QComboBox
Thank You, that was the bit of information I was needing. It was a FU
NDAMENTAL lack of knowledge.