Results 1 to 3 of 3

Thread: display a line number of my tableView not working

  1. #1
    Join Date
    Dec 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default display a line number of my tableView not working

    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
    Capture.jpg

    and the problem is that label_9 is not taking any number
    Capture.PNG

    i used also
    Qt Code:
    1. int k = ui->tableView->currentIndex().row();
    2. ui->label_9->setNum(k)
    To copy to clipboard, switch view to plain text mode 

    and the lable is keep showing me -1 !

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: display a line number of my tableView not working

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: display a line number of my tableView not working

    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
    Qt Code:
    1. void personnel_click::arrow()
    2. {
    3.  
    4. int k = ui->tableView->currentIndex().row();
    5. ui->label_9->setNum(k);
    6. ui->label_9->update();
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 




    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





    Qt Code:
    1. #include "personnel_click.h"
    2. #include "ui_personnel_click.h"
    3. #include "ajouter_p.h"
    4. #include "afficher_p.h"
    5. #include "supprimer_p.h"
    6. #include "personnel.h"
    7. #include <QSqlDatabase>
    8. #include <QMessageBox>
    9. #include <QSqlQuery>
    10. #include <QSqlQueryModel>
    11. #include <bien.h>
    12.  
    13.  
    14.  
    15. personnel_click::personnel_click(QWidget *parent) :
    16. QDialog(parent),
    17. ui(new Ui::personnel_click)
    18. {
    19. ui->setupUi(this);
    20. }
    21.  
    22. personnel_click::~personnel_click()
    23. {
    24. delete ui;
    25. }
    26.  
    27. void personnel_click::on_pushButton_clicked()
    28. {
    29. ui->stackedWidget->setCurrentIndex(1);
    30.  
    31. }
    32.  
    33. void personnel_click::on_pushButton_4_clicked()
    34. {
    35. mentionner();
    36. ui->tableView->setColumnHidden(0,true);
    37. arrow();
    38. }
    39.  
    40.  
    41. void personnel_click::on_pushButton_3_clicked()
    42. {
    43.  
    44. QMessageBox::StandardButton reply;
    45. reply = QMessageBox::question(this, "suppression de personnel", "are you sure you want to delete this personnel?",
    46. QMessageBox::Yes|QMessageBox::No);
    47. if (reply == QMessageBox::Yes) {
    48. Personnel p ;
    49. int i = ui->tableView->model()->index(ui->tableView->currentIndex().row(),0).data().toInt();
    50. p.supprimer(i);
    51. p.son_2();
    52. on_pushButton_4_clicked();
    53. }
    54.  
    55.  
    56. }
    57. void personnel_click::arrow()
    58. {
    59.  
    60. int k = ui->tableView->currentIndex().row();
    61. ui->label_9->setNum(k);
    62. ui->label_9->update();
    63.  
    64. }
    65.  
    66. void personnel_click::on_pushButton_5_clicked()
    67. {
    68.  
    69. QString horaire=ui->horaire->text();
    70. QString nom=ui->nom->text();
    71. //QString domaine=ui->domaine->text();
    72. QString prix=ui->salaire->text();
    73. QString dispo="";
    74.  
    75. bool etat = 0;
    76. QRadioButton *qrb = ui->radioButton_2;
    77. //qrb->setChecked(false);
    78. if( qrb ->isChecked())
    79. etat = 1;
    80. else etat=0 ;
    81. if (etat==1)
    82. dispo="disponible";
    83. else dispo="indisponible";
    84. QString dom=ui->comboBox->currentText();
    85.  
    86. int appresiation=5;
    87. Personnel x(nom,horaire,etat,dispo,dom,prix,appresiation);
    88. bool test=x.ajouterPersonnel();
    89. if(test)
    90. {
    91.  
    92. p.son();
    93.  
    94. }
    95. else
    96. {
    97. QMessageBox::critical(nullptr, QObject::tr("Supprimer un Abonne"),
    98. QObject::tr("Erreur !.\n"
    99. "Click Cancel to exit."), QMessageBox::Cancel);
    100.  
    101. }
    102. ui->horaire->clear();
    103. ui->nom->clear();
    104. ui->nom->clear();
    105. on_pushButton_4_clicked();
    106.  
    107.  
    108. }
    109. void personnel_click::mentionner()
    110. {
    111. QSqlQuery *request = new QSqlQuery();
    112. request->prepare("SELECT * FROM PERSONNEL");
    113. request->exec();
    114. model->setQuery(*request);
    115.  
    116. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID_P"));
    117. model->setHeaderData(1, Qt::Horizontal, QObject::tr("NOM"));
    118. model->setHeaderData(2, Qt::Horizontal, QObject::tr("APPRESTATION_P"));
    119. model->setHeaderData(3, Qt::Horizontal, QObject::tr("ETAT"));
    120. model->setHeaderData(4, Qt::Horizontal, QObject::tr("HORAIRE_P"));
    121. model->setHeaderData(5, Qt::Horizontal, QObject::tr("PRIX"));
    122. model->setHeaderData(6, Qt::Horizontal, QObject::tr("DOMAINE"));
    123.  
    124.  
    125. ui->tableView->setModel(model);
    126. }
    127.  
    128.  
    129.  
    130.  
    131.  
    132.  
    133. void personnel_click::on_pushButton_2_clicked()
    134. {
    135. ui->stackedWidget->setCurrentIndex(2);
    136.  
    137. }
    138.  
    139. void personnel_click::on_pushButton_6_clicked()
    140. {
    141. Personnel p ;
    142. int i = ui->tableView->model()->index(ui->tableView->currentIndex().row(),0).data().toInt();
    143. QString prix=ui->salaire_2->text();
    144. QString horaire=ui->horaire_2->text();
    145. bool etat = 0;
    146. QString dispo ;
    147. QRadioButton *qrb = ui->radioButton_4;
    148. //qrb->setChecked(false);
    149. if( qrb ->isChecked())
    150. etat = 1;
    151. else etat=0 ;
    152. if (etat==1)
    153. dispo="disponible";
    154. else dispo="indisponible";
    155. p.modifier(i,prix,horaire,dispo);
    156. on_pushButton_4_clicked();
    157.  
    158. }
    159. void personnel_click::on_lineEdit_textChanged(const QString &arg1)
    160. {
    161. QSqlQuery *q = new QSqlQuery ();
    162. ui->text->setText(arg1);
    163.  
    164. q->prepare("SELECT * FROM PERSONNEL WHERE "+ui->comboBox_2->currentText()+" LIKE '"+arg1+"%'");
    165. q->exec();
    166. model->setQuery(*q);
    167. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID_P"));
    168. model->setHeaderData(1, Qt::Horizontal, QObject::tr("NOM"));
    169. model->setHeaderData(2, Qt::Horizontal, QObject::tr("APPRESTATION_P"));
    170. model->setHeaderData(3, Qt::Horizontal, QObject::tr("ETAT"));
    171. model->setHeaderData(4, Qt::Horizontal, QObject::tr("HORAIRE_P"));
    172. model->setHeaderData(5, Qt::Horizontal, QObject::tr("PRIX"));
    173. model->setHeaderData(6, Qt::Horizontal, QObject::tr("DOMAINE"));
    174.  
    175. ui->tableView->setModel(model);
    176. ui->tableView->setColumnHidden(0,true);
    177.  
    178. }
    179.  
    180.  
    181. void personnel_click::on_pushButton_7_clicked()
    182. {
    183. bien b;
    184. hide();
    185. b.exec();
    186. b.show();
    187. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 20th January 2016, 01:40
  2. How do I set a QTextEdit to a specific line by line number?
    By Coolname007 in forum Qt Programming
    Replies: 8
    Last Post: 1st February 2013, 05:18
  3. How to get Line Number in QWebView
    By nish in forum Qt Programming
    Replies: 0
    Last Post: 16th December 2009, 13:02
  4. How to get row number from tableView
    By rogerholmes in forum Newbie
    Replies: 2
    Last Post: 26th March 2009, 16:30
  5. How to get column number in a line
    By showhand in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 09:42

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.