View Full Version : QComboBox Problem
anouar2002
19th January 2012, 03:52
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.
monst
19th January 2012, 05:51
Do you want to set another current item?
Use
void setCurrentIndex(int index).
anouar2002
19th January 2012, 15:14
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:
this->model = new QSqlQueryModel();
model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
ui->comboBox->setModel(model);
this->model2 = new QSqlQueryModel();
model2->setQuery("SELECT DISTINCT N_Four FROM Projet.dbo.Produit");
ui->comboBox_2->setModel(model2);
QSqlQuery query5;
query5.prepare("SELECT N_Four FROM Projet.dbo.Produit WHERE Nom=:nom");
query5.bindValue(":nom", ui->comboBox->currentText());
if (query5.exec() && query5.next()) {
ui->comboBox_2->setItemText(0,query5.value(0).toString());
}
void fiche_article::on_comboBox_currentIndexChanged(con st QString &arg1)
{
QSqlQuery query5;
query5.prepare("SELECT N_Four FROM Projet.dbo.Produit WHERE Nom=:nom");
query5.bindValue(":nom", ui->comboBox->currentText());
if (query5.exec() && query5.next()) {
ui->comboBox_2->setItemText(0,query5.value(0).toString());
}
monst
19th January 2012, 20:58
my_widget.h:
#include <QtGui>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent = 0)
{
QHBoxLayout* layout = new QHBoxLayout(this);
combo1 = new QComboBox(this);
combo2 = new QComboBox(this);
layout->addWidget(combo1);
layout->addWidget(combo2);
combo1->addItem("combo_item1");
combo1->addItem("combo_item2");
combo2->addItem("combo_item3");
combo2->addItem("combo_item4");
connect(combo1, SIGNAL(currentIndexChanged(const QString&)),
this, SLOT(_onCombo1_currentIndexChanged(const QString&)));
}
private slots:
void _onCombo1_currentIndexChanged(const QString &arg1)
{
QString val("trololo");
//take value from database here...
combo2->setItemText(0, val);
}
private:
QComboBox* combo1;
QComboBox* combo2;
};
main.cpp:
#include "my_widget.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}
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?
anouar2002
19th January 2012, 23:14
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 :
#include "fiche_article.h"
#include "ui_fiche_article.h"
#include <QtDebug>
fiche_article::fiche_article(QWidget *parent) :
QDialog(parent),
ui(new Ui::fiche_article)
{
ui->setupUi(this);
this->model = new QSqlQueryModel();
model->setQuery("SELECT Nom FROM Projet.dbo.Produit");
ui->comboBox->setModel(model);
QSqlQuery query;
query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
query.bindValue(":nom", ui->comboBox->currentText());
if (query.exec() && query.next()) {
ui->lineEdit_3->setText(query.value(0).toString());
}
// QObject::connect(ui->comboBox, SIGNAL(currentIndexChanged(int)),ui->lineEdit_3, SLOT(setText(ui->comboBox->currentText())));
QSqlQuery query2;
query2.prepare("SELECT Nom FROM Projet.dbo.Produit WHERE Nom=:nom");
query2.bindValue(":nom", ui->comboBox->currentText());
if (query2.exec() && query2.next()) {
ui->lineEdit->setText(query2.value(0).toString());
}
QSqlQuery query3;
query3.prepare("SELECT Type FROM Projet.dbo.Produit WHERE Nom=:nom");
query3.bindValue(":nom", ui->comboBox->currentText());
if (query3.exec() && query3.next()) {
ui->lineEdit_2->setText(query3.value(0).toString());
}
QSqlQuery query4;
query4.prepare("SELECT Quantité FROM Projet.dbo.Produit WHERE Nom=:nom");
query4.bindValue(":nom", ui->comboBox->currentText());
if (query4.exec() && query4.next()) {
ui->spinBox->setValue(query4.value(0).toInt());
}
this->model2 = new QSqlQueryModel();
model2->setQuery("SELECT DISTINCT N_Four FROM Projet.dbo.Produit");
ui->comboBox_2->setModel(model2);
QSqlQuery query5;
query5.prepare("SELECT N_Four FROM Projet.dbo.Produit WHERE Nom=:nom");
query5.bindValue(":nom", ui->comboBox->currentText());
if (query5.exec() && query5.next()) {
ui->comboBox_2->setItemText(0,query5.value(0).toString());
}
}
fiche_article::~fiche_article()
{
delete ui;
}
void fiche_article::on_comboBox_currentIndexChanged(con st QString &arg1)
{
QSqlQuery query;
query.prepare("SELECT Matricule FROM Projet.dbo.Produit WHERE Nom=:nom");
query.bindValue(":nom", ui->comboBox->currentText());
if (query.exec() && query.next()) {
ui->lineEdit_3->setText(query.value(0).toString());
}
QSqlQuery query3;
query3.prepare("SELECT Type FROM Projet.dbo.Produit WHERE Nom=:nom");
query3.bindValue(":nom", ui->comboBox->currentText());
if (query3.exec() && query3.next()) {
ui->lineEdit_2->setText(query3.value(0).toString());
}
QSqlQuery query4;
query4.prepare("SELECT Quantité FROM Projet.dbo.Produit WHERE Nom=:nom");
query4.bindValue(":nom", ui->comboBox->currentText());
if (query4.exec() && query4.next()) {
ui->spinBox->setValue(query4.value(0).toInt());
}
}
fiche_article.h :
#ifndef FICHE_ARTICLE_H
#define FICHE_ARTICLE_H
#include <QDialog>
#include <QtGui>
#include <QtSql>
#include <QtCore>
namespace Ui {
class fiche_article;
}
class fiche_article : public QDialog
{
Q_OBJECT
public:
explicit fiche_article(QWidget *parent = 0);
~fiche_article();
private slots:
void on_comboBox_currentIndexChanged(const QString &arg1);
private:
Ui::fiche_article *ui;
QSqlDatabase db;
QSqlQuery *qry;
QString *sQuery;
QSqlQueryModel *model;
QSqlQueryModel *model2;
};
#endif // FICHE_ARTICLE_H
main.cpp :
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
and there are many others pages ;)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.