Results 1 to 7 of 7

Thread: How to get value from QComboBox

  1. #1
    Join Date
    Jun 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Jun 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get value from QComboBox

    All this code is in MainWindow class. In the header I define modelCell as
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. QSqlQueryModel *modelCell;
    To copy to clipboard, switch view to plain text mode 
    Then I create the pointer. I thought this would allow the pointer to be used in any of the functions of the mainwindow class.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. QSqlQueryModel *modelCell = new QSqlQueryModel(this);
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. void MainWindow::on_btnSaveConductivityTest_clicked()
    2. {
    3. QSqlRecord record = modelCell->record(ui->cmbCellList->currentIndex());
    4. QString lstr;
    5. lstr = record.value(0).toString();
    6. ui->lineEdit->setText(lstr);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2006
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get value from QComboBox

    May Be data is there at
    Qt Code:
    1. record.value(0).toString()
    To copy to clipboard, switch view to plain text mode 
    or
    Check
    Qt Code:
    1. ui->cmbCellList->currentIndex()
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to get value from QComboBox

    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:
    Qt Code:
    1. modelCell = new QSqlQueryModel(this);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. Set value to QComboBox
    By Archa4 in forum Newbie
    Replies: 2
    Last Post: 19th May 2011, 12:46
  2. QComboBox
    By kavinsiva in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2009, 16:11
  3. QComboBox
    By bismitapadhy in forum Qt Programming
    Replies: 7
    Last Post: 15th July 2009, 06:01
  4. QComboBox
    By coderbob in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2007, 06:38
  5. QComboBox
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2007, 13:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.