PDA

View Full Version : display a line number of my tableView not working



aziz sahnoun
25th December 2018, 23:02
Hi, i'm trying here to display on my label_9 the number of the selected line of my table so i used this code
12999

and the problem is that label_9 is not taking any number
13000

i used also

int k = ui->tableView->currentIndex().row();
ui->label_9->setNum(k)

and the lable is keep showing me -1 !

d_stranz
26th December 2018, 00:33
When you run your code in the debugger and get to this line:


int k = ui->tableView->currentIndex().row();

what does the debugger tell you the value of k" is?


int k = ui->tableView->currentIndex().row();
ui->label_9->setNum(k)

Where does this code appear? In a slot, in a constructor, somewhere else? My magic code viewer isn't working today, so trying to determine what is wrong with your code won't be easy unless you post more of it.

aziz sahnoun
26th December 2018, 00:49
thnx for your Reply
well my goal is to have an update on label_9 and to show the current row of the selected line in the tableView
this is my current code for now

void personnel_click::arrow()
{

int k = ui->tableView->currentIndex().row();
ui->label_9->setNum(k);
ui->label_9->update();

}




and yes this code appear in a slot ; my k is taking always the value of -1 even when i select another line in the table
and here all the .cpp






#include "personnel_click.h"
#include "ui_personnel_click.h"
#include "ajouter_p.h"
#include "afficher_p.h"
#include "supprimer_p.h"
#include "personnel.h"
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <bien.h>



personnel_click::personnel_click(QWidget *parent) :
QDialog(parent),
ui(new Ui::personnel_click)
{
ui->setupUi(this);
}

personnel_click::~personnel_click()
{
delete ui;
}

void personnel_click::on_pushButton_clicked()
{
ui->stackedWidget->setCurrentIndex(1);

}

void personnel_click::on_pushButton_4_clicked()
{
mentionner();
ui->tableView->setColumnHidden(0,true);
arrow();
}


void personnel_click::on_pushButton_3_clicked()
{

QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "suppression de personnel", "are you sure you want to delete this personnel?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
Personnel p ;
int i = ui->tableView->model()->index(ui->tableView->currentIndex().row(),0).data().toInt();
p.supprimer(i);
p.son_2();
on_pushButton_4_clicked();
}


}
void personnel_click::arrow()
{

int k = ui->tableView->currentIndex().row();
ui->label_9->setNum(k);
ui->label_9->update();

}

void personnel_click::on_pushButton_5_clicked()
{

QString horaire=ui->horaire->text();
QString nom=ui->nom->text();
//QString domaine=ui->domaine->text();
QString prix=ui->salaire->text();
QString dispo="";

bool etat = 0;
QRadioButton *qrb = ui->radioButton_2;
//qrb->setChecked(false);
if( qrb ->isChecked())
etat = 1;
else etat=0 ;
if (etat==1)
dispo="disponible";
else dispo="indisponible";
QString dom=ui->comboBox->currentText();

int appresiation=5;
Personnel x(nom,horaire,etat,dispo,dom,prix,appresiation);
bool test=x.ajouterPersonnel();
if(test)
{

p.son();

}
else
{
QMessageBox::critical(nullptr, QObject::tr("Supprimer un Abonne"),
QObject::tr("Erreur !.\n"
"Click Cancel to exit."), QMessageBox::Cancel);

}
ui->horaire->clear();
ui->nom->clear();
ui->nom->clear();
on_pushButton_4_clicked();


}
void personnel_click::mentionner()
{
QSqlQueryModel *model = new QSqlQueryModel();
QSqlQuery *request = new QSqlQuery();
request->prepare("SELECT * FROM PERSONNEL");
request->exec();
model->setQuery(*request);

model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID_P"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("NOM"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("APPRESTATION_P"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("ETAT"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("HORAIRE_P"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("PRIX"));
model->setHeaderData(6, Qt::Horizontal, QObject::tr("DOMAINE"));


ui->tableView->setModel(model);
}






void personnel_click::on_pushButton_2_clicked()
{
ui->stackedWidget->setCurrentIndex(2);

}

void personnel_click::on_pushButton_6_clicked()
{
Personnel p ;
int i = ui->tableView->model()->index(ui->tableView->currentIndex().row(),0).data().toInt();
QString prix=ui->salaire_2->text();
QString horaire=ui->horaire_2->text();
bool etat = 0;
QString dispo ;
QRadioButton *qrb = ui->radioButton_4;
//qrb->setChecked(false);
if( qrb ->isChecked())
etat = 1;
else etat=0 ;
if (etat==1)
dispo="disponible";
else dispo="indisponible";
p.modifier(i,prix,horaire,dispo);
on_pushButton_4_clicked();

}
void personnel_click::on_lineEdit_textChanged(const QString &arg1)
{
QSqlQuery *q = new QSqlQuery ();
QSqlQueryModel *model = new QSqlQueryModel();
ui->text->setText(arg1);

q->prepare("SELECT * FROM PERSONNEL WHERE "+ui->comboBox_2->currentText()+" LIKE '"+arg1+"%'");
q->exec();
model->setQuery(*q);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID_P"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("NOM"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("APPRESTATION_P"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("ETAT"));
model->setHeaderData(4, Qt::Horizontal, QObject::tr("HORAIRE_P"));
model->setHeaderData(5, Qt::Horizontal, QObject::tr("PRIX"));
model->setHeaderData(6, Qt::Horizontal, QObject::tr("DOMAINE"));

ui->tableView->setModel(model);
ui->tableView->setColumnHidden(0,true);

}


void personnel_click::on_pushButton_7_clicked()
{
bien b;
hide();
b.exec();
b.show();
}