Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

My objects aren't working.



  • I'm finishing an interface with QT, and I've created another new window. The fact is that I have added two QComboBox, each with the function of showing a table in a database (each QComboBox shows a different table). When selecting in the QComboBox, I should show the data of that table in the fields that I have below. Also I added a button below that what I would do would be to create an entry to another table.

    The problem comes when I try to show information after I have started the window. By default when I open it, I am shown the user information that I have placed by default in the QComboBox, but if I select another user, my information is not shown, and my button does not create the new entry in the table either. I'm a little lost because I do not know what to do. Any solution? Thank you!



  • @AdrianCruz Please don't double post, here's your same question in Spanish...



  • @Pablo-J.-Rogina I have deleted the another one. :)



  • Hi,
    @AdrianCruz said in My objects aren't working.:

    I do not know what to do.

    First thing to do is read and understand the Application Output.



  • @LeLev Ok, doing test, i can conclude that isn't a problem about databases. Is a problem about my widget, why? Because i have a sumple bottom in the widget and I developed this bottom with the function close() to close the widget, and this bottom isn't working too. QComboBox aren't working, bottom too..



  • file.h

    #ifndef ENLAZARPACIENTEESPECIALISTA_H
    #define ENLAZARPACIENTEESPECIALISTA_H
    
    #include <QWidget>
    #include <QSqlQuery>
    #include <QMessageBox>
    #include <QDebug>
    
    namespace Ui {
    class enlazarpacienteespecialista;
    }
    
    class enlazarpacienteespecialista : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit enlazarpacienteespecialista(QWidget *parent = 0);
        ~enlazarpacienteespecialista();
    
    private:
        Ui::enlazarpacienteespecialista *ui;
        QString paciente;
        QString especialista;
    
    protected:
        void resizeEvent(QResizeEvent *evt);
    private slots:
        void on_paciente_activated(const QString &arg1);
        void on_pushButton_clicked();
        void on_especialista_activated(const QString &arg1);
        void on_enlazar_clicked();
    };
    
    #endif // ENLAZARPACIENTEESPECIALISTA_H
    
    

    file.cpp

    #include "enlazarpacienteespecialista.h"
    #include "ui_enlazarpacienteespecialista.h"
    
    enlazarpacienteespecialista::enlazarpacienteespecialista(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::enlazarpacienteespecialista)
    {
        ui->setupUi(this);
        ui->setupUi(this);
        QPixmap bkgnd("../InterfazKinect/img/foto_index_sinTXT.png");
        bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
        QPalette palette;
        palette.setBrush(QPalette::Background, bkgnd);
        this->setPalette(palette);
    
        int count1 = 0, count2 = 0;
            QSqlQuery query,query2;
            query.exec("Select dni_paciente,nombre_paciente,apellidos_paciente,fecha_nacimiento from paciente");
            while(query.next()){
                ui->paciente->addItem(query.value(0).toString());
    
                if(count1 == 0){
                    paciente = query.value(0).toString();
                    ui->nombrePaciente->setText(query.value(1).toString());
                    ui->apellidosPaciente->setText(query.value(2).toString());
                    QDate Date = QDate::fromString(query.value(3).toString(),"yyyy-MM-dd");
                    ui->fechaNacimiento->setDate(Date);
                    count1 = 1; //Control para mostrar el primero.
                }
            }
    
    
            query2.exec("Select dni_especialista,nombre_especialista,apellidos_especialista,tipo_especialidad from especialista");
            while(query2.next()){
                ui->especialista->addItem(query2.value(0).toString());
    
                if(count2 == 0){
                    especialista = query2.value(0).toString();
                    ui->nombreEspecialista->setText(query2.value(1).toString());
                    ui->apellidosEspecialista->setText(query2.value(2).toString());
                    ui->tipoEspecialidad->setText(query2.value(3).toString());
                    count2 = 1; //Control para mostrar el primero
                }
            }
    
    }
    
    enlazarpacienteespecialista::~enlazarpacienteespecialista()
    {
        delete ui;
    }
    
    void enlazarpacienteespecialista::resizeEvent(QResizeEvent *evt)
    {
        QPixmap bkgnd("../InterfazKinect/img/foto_index_sinTXT.png");//Load pic
        bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);//set scale of pic to match the window
        QPalette p = palette(); //copy current, not create new
        p.setBrush(QPalette::Background, bkgnd);//set the pic to the background
        setPalette(p);//show the background pic
    
        QWidget::resizeEvent(evt); //call base implementation
    }
    
    
    
    
    void enlazarpacienteespecialista::on_paciente_activated(const QString &arg1)
    {
                paciente = arg1;
                int existe = 0;
    
                ui->nombrePaciente->clear();
                ui->apellidosPaciente->clear();
                ui->fechaNacimiento->clear();
    
                QSqlQuery buscar;
    
                if(buscar.exec("SELECT dni_paciente,nombre_paciente, apellidos_paciente, fecha_nacimiento from paciente")){
    
                    while(buscar.next() && existe == 0){
                        if(paciente == buscar.value(0).toString()){
                            existe=1;
    
                            ui->nombrePaciente->setText(buscar.value(1).toString());
                            ui->apellidosPaciente->setText(buscar.value(2).toString());
                            QDate DateNew = QDate::fromString(buscar.value(3).toString(),"yyyy-MM-dd");
                            ui->fechaNacimiento->setDate(DateNew);
                        }
                    }
                }else{
                    QMessageBox::warning(this,"Sin información",tr("No se pudo mostrar la información."));
                }
    
    
    }
    
    void enlazarpacienteespecialista::on_pushButton_clicked()
    {
        close();
    }
    
    void enlazarpacienteespecialista::on_especialista_activated(const QString &arg1)
    {
    
                especialista = arg1;
    
                ui->nombreEspecialista->clear();
                ui->apellidosEspecialista->clear();
                ui->tipoEspecialidad->clear();
    
                QSqlQuery busqueda;
                int existe=0;
    
                if (!busqueda.exec("Select nombre_especialista ,apellidos_especialista ,tipo_especialidad, dni_especialista from especialista")){
                    QMessageBox::warning(this,"Sin información",tr("La información no pudo ser mostrada."));
                }else{
    
                    while(busqueda.next() && existe == 0){
    
                        if(busqueda.value(3).toString() == especialista){
                            existe = 1;
    
                            ui->nombreEspecialista->setText(busqueda.value(0).toString());
                            ui->apellidosEspecialista->setText(busqueda.value(1).toString());
                            ui->tipoEspecialidad->setText(busqueda.value(2).toString());
                        }
                    }
                }
    
    }
    
    void enlazarpacienteespecialista::on_enlazar_clicked()
    {
                int exist=0;
                QString buscaDNIpaciente,buscaDNIespecialista;
                QSqlQuery consulta;
    
                if(!consulta.exec("Select dni_paciente,dni_especialista from paciente_especialista")){
                     QMessageBox::warning(this, "Error al obtener los DNIs de la base de datos.",tr("No se pudieron obtener los datos"));
                }else{
    
                     while(consulta.next()){
                         buscaDNIpaciente = consulta.value(0).toString();
                        buscaDNIespecialista = consulta.value(1).toString();
    
                        if (buscaDNIespecialista == especialista && buscaDNIpaciente == paciente){
                             exist=1;
                        }
                    }
                }
    
                if(exist == 0){
                     consulta.prepare("INSERT INTO paciente_especialista(dni_paciente,dni_especialista)"
                                    "VALUES (:dni_paciente, :dni_especialista)");
                    consulta.bindValue(":dni_paciente", paciente);
                    consulta.bindValue(":dni_especialista",especialista);
    
                    if(!consulta.exec()){
                        QMessageBox::warning(this, "Error al insertar los DNIs de la base de datos.",tr("No se pudieron insertar los datos"));
                    }else{
                        QMessageBox::warning(this, "Usuarios enlazados.",tr("El paciente y el especialista ahora están asociados."));
                    }
                }else{
                    QMessageBox::warning(this, "Usuarios anteriormente enlazados.",tr("El paciente y el especialista ya estaban asociados."));
                }
    
    }
    

    This is my code, still isn't working.



  • I solved the problem! I repeated "ui->setupUi(this);" and for that reason wasn't working. If anyone can explain me why that is problem, i will be grateful. Thanks for all.


  • Lifetime Qt Champion

    Hi,

    setupUi as its name suggests does the initialisation of your widget, so calling it twice is a bit like trying to start your car engine when it's already running.


Log in to reply