Results 1 to 5 of 5

Thread: QComboBox Problem

  1. #1
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QComboBox Problem

    Hello!
    How to choose what is the value of QComboBox which I want to be displayed in the loading of the interface.
    I read the documentation and I found this method "setItemText (int index, QString text)" as I used to index 0 for the value to be displayed first but it result nothing.
    What should I do? Thank you in advance
    NB: the values ​​of my QComboBox are loaded from a database.

  2. #2
    Join Date
    Jan 2012
    Location
    St. Petersburg, Russia
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QComboBox Problem

    Do you want to set another current item?
    Use
    Qt Code:
    1. void setCurrentIndex(int index)
    To copy to clipboard, switch view to plain text mode 
    .

  3. #3
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox Problem

    Thank you for your response.
    The problem is that my comboBox is bound by one slot to another comboBox_Global ie when I change the value of my ComboxBox_Global, the value of comboBox is not changed
    As if there is no synchronization between them.
    This is the code:
    Qt Code:
    1. this->model = new QSqlQueryModel();
    2. model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
    3. ui->comboBox->setModel(model);
    4. this->model2 = new QSqlQueryModel();
    5. model2->setQuery("SELECT DISTINCT N_Four FROM Projet.dbo.Produit");
    6. ui->comboBox_2->setModel(model2);
    7.  
    8.  
    9. QSqlQuery query5;
    10. query5.prepare("SELECT N_Four FROM Projet.dbo.Produit WHERE Nom=:nom");
    11. query5.bindValue(":nom", ui->comboBox->currentText());
    12. if (query5.exec() && query5.next()) {
    13. ui->comboBox_2->setItemText(0,query5.value(0).toString());
    14.  
    15. }
    16. void fiche_article::on_comboBox_currentIndexChanged(const QString &arg1)
    17. {
    18.  
    19.  
    20. QSqlQuery query5;
    21. query5.prepare("SELECT N_Four FROM Projet.dbo.Produit WHERE Nom=:nom");
    22. query5.bindValue(":nom", ui->comboBox->currentText());
    23. if (query5.exec() && query5.next()) {
    24. ui->comboBox_2->setItemText(0,query5.value(0).toString());
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2012
    Location
    St. Petersburg, Russia
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: QComboBox Problem

    my_widget.h:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyWidget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. MyWidget(QWidget* parent = 0)
    8. {
    9. QHBoxLayout* layout = new QHBoxLayout(this);
    10. combo1 = new QComboBox(this);
    11. combo2 = new QComboBox(this);
    12.  
    13. layout->addWidget(combo1);
    14. layout->addWidget(combo2);
    15.  
    16. combo1->addItem("combo_item1");
    17. combo1->addItem("combo_item2");
    18. combo2->addItem("combo_item3");
    19. combo2->addItem("combo_item4");
    20.  
    21. connect(combo1, SIGNAL(currentIndexChanged(const QString&)),
    22. this, SLOT(_onCombo1_currentIndexChanged(const QString&)));
    23. }
    24.  
    25. private slots:
    26. void _onCombo1_currentIndexChanged(const QString &arg1)
    27. {
    28. QString val("trololo");
    29. //take value from database here...
    30.  
    31. combo2->setItemText(0, val);
    32. }
    33.  
    34. private:
    35. QComboBox* combo1;
    36. QComboBox* combo2;
    37. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp:

    Qt Code:
    1. #include "my_widget.h"
    2.  
    3. int main(int argc, char** argv)
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. MyWidget w;
    8. w.show();
    9.  
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    This example works fine for me.
    It's will be much harder to find a problem if your actual problem is in your database code.
    But synchronization between two combo boxes is not a problem.

    If I've got you wrong, can you tell me:
    what is a value that query5.value(0).toString() returns?
    was ui variable constructed?
    was setupUi method called?

  5. The following user says thank you to monst for this useful post:

    anouar2002 (19th January 2012)

  6. #5
    Join Date
    Jan 2012
    Posts
    25
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox Problem

    Hello, thank you for answering me again.

    I didn't understand your code perfectly. In fact, i can't see the utility of creating everything on header( i use the header only for declaration)
    I will answer your questions :
    query5.value(0).toString returns the num_Four from my base
    ui is the mainwindow widget
    and i will post the hole code here :

    fiche_article.cpp :
    Qt Code:
    1. #include "fiche_article.h"
    2. #include "ui_fiche_article.h"
    3. #include <QtDebug>
    4.  
    5. fiche_article::fiche_article(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::fiche_article)
    8. {
    9. ui->setupUi(this);
    10. this->model = new QSqlQueryModel();
    11. model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
    12. ui->comboBox->setModel(model);
    13.  
    14.  
    15. QSqlQuery query;
    16. query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
    17. query.bindValue(":nom", ui->comboBox->currentText());
    18. if (query.exec() && query.next()) {
    19. ui->lineEdit_3->setText(query.value(0).toString());
    20. }
    21. // QObject::connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),ui->lineEdit_3, SLOT(setText(ui->comboBox->currentText())));
    22.  
    23. QSqlQuery query2;
    24. query2.prepare("SELECT Nom FROM Projet.dbo.Produit WHERE Nom=:nom");
    25. query2.bindValue(":nom", ui->comboBox->currentText());
    26. if (query2.exec() && query2.next()) {
    27. ui->lineEdit->setText(query2.value(0).toString());
    28. }
    29.  
    30.  
    31. QSqlQuery query3;
    32. query3.prepare("SELECT Type FROM Projet.dbo.Produit WHERE Nom=:nom");
    33. query3.bindValue(":nom", ui->comboBox->currentText());
    34. if (query3.exec() && query3.next()) {
    35. ui->lineEdit_2->setText(query3.value(0).toString());
    36.  
    37. }
    38.  
    39. QSqlQuery query4;
    40. query4.prepare("SELECT Quantité FROM Projet.dbo.Produit WHERE Nom=:nom");
    41. query4.bindValue(":nom", ui->comboBox->currentText());
    42. if (query4.exec() && query4.next()) {
    43. ui->spinBox->setValue(query4.value(0).toInt());
    44.  
    45.  
    46. }
    47. this->model2 = new QSqlQueryModel();
    48. model2->setQuery("SELECT DISTINCT N_Four FROM Projet.dbo.Produit");
    49. ui->comboBox_2->setModel(model2);
    50.  
    51.  
    52. QSqlQuery query5;
    53. query5.prepare("SELECT N_Four FROM Projet.dbo.Produit WHERE Nom=:nom");
    54. query5.bindValue(":nom", ui->comboBox->currentText());
    55. if (query5.exec() && query5.next()) {
    56. ui->comboBox_2->setItemText(0,query5.value(0).toString());
    57.  
    58. }
    59.  
    60.  
    61. }
    62.  
    63. fiche_article::~fiche_article()
    64. {
    65. delete ui;
    66. }
    67.  
    68. void fiche_article::on_comboBox_currentIndexChanged(const QString &arg1)
    69. {
    70.  
    71. QSqlQuery query;
    72. query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
    73. query.bindValue(":nom", ui->comboBox->currentText());
    74. if (query.exec() && query.next()) {
    75. ui->lineEdit_3->setText(query.value(0).toString());
    76. }
    77. QSqlQuery query3;
    78. query3.prepare("SELECT Type FROM Projet.dbo.Produit WHERE Nom=:nom");
    79. query3.bindValue(":nom", ui->comboBox->currentText());
    80. if (query3.exec() && query3.next()) {
    81. ui->lineEdit_2->setText(query3.value(0).toString());
    82. }
    83.  
    84. QSqlQuery query4;
    85. query4.prepare("SELECT Quantité FROM Projet.dbo.Produit WHERE Nom=:nom");
    86. query4.bindValue(":nom", ui->comboBox->currentText());
    87. if (query4.exec() && query4.next()) {
    88. ui->spinBox->setValue(query4.value(0).toInt());
    89.  
    90. }
    91.  
    92.  
    93. }
    To copy to clipboard, switch view to plain text mode 

    fiche_article.h :
    Qt Code:
    1. #ifndef FICHE_ARTICLE_H
    2. #define FICHE_ARTICLE_H
    3.  
    4. #include <QDialog>
    5. #include <QtGui>
    6. #include <QtSql>
    7. #include <QtCore>
    8.  
    9. namespace Ui {
    10. class fiche_article;
    11. }
    12.  
    13. class fiche_article : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit fiche_article(QWidget *parent = 0);
    19. ~fiche_article();
    20.  
    21. private slots:
    22. void on_comboBox_currentIndexChanged(const QString &arg1);
    23.  
    24. private:
    25. Ui::fiche_article *ui;
    26. QSqlQuery *qry;
    27. QString *sQuery;
    28.  
    29.  
    30.  
    31. QSqlQueryModel *model2;
    32.  
    33.  
    34. };
    35.  
    36. #endif // FICHE_ARTICLE_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp :
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    and there are many others pages

Similar Threads

  1. Problem with QComboBox
    By wagmare in forum Newbie
    Replies: 4
    Last Post: 1st December 2011, 10:25
  2. QComboBox problem
    By SteveH in forum Newbie
    Replies: 2
    Last Post: 2nd March 2009, 21:20
  3. QComboBox problem
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 31st March 2008, 11:43
  4. Problem while using QComboBox
    By merry in forum Qt Programming
    Replies: 6
    Last Post: 20th December 2007, 10:22
  5. Problem with QComboBox
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 8th December 2007, 13:21

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.