PDA

View Full Version : How to get value from QComboBox



talmage
17th November 2011, 16:39
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.

ChrisW67
17th November 2011, 22:52
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.

talmage
18th November 2011, 14:21
All this code is in MainWindow class. In the header I define modelCell as

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QSqlQueryModel *modelCell;
Then I create the pointer. I thought this would allow the pointer to be used in any of the functions of the mainwindow class.


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlQueryModel *modelCell = new QSqlQueryModel(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.


void MainWindow::on_btnSaveConductivityTest_clicked()
{
QSqlRecord record = modelCell->record(ui->cmbCellList->currentIndex());
QString lstr;
lstr = record.value(0).toString();
ui->lineEdit->setText(lstr);

basha
18th November 2011, 14:46
May Be data is there at
record.value(0).toString()
or
Check
ui->cmbCellList->currentIndex()

talmage
30th November 2011, 13:44
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.

norobro
30th November 2011, 19:40
ChrisW67 gave you the answer in post #2:
. . . 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:
modelCell = new QSqlQueryModel(this);

talmage
1st December 2011, 14:11
Thank You, that was the bit of information I was needing. It was a FU
NDAMENTAL lack of knowledge.