Results 1 to 2 of 2

Thread: creating your own SLOT

  1. #1
    Join Date
    Jul 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default creating your own SLOT

    Hello,
    I'm creating my first app with my own slot .

    I'm trying to program an app able to manage salaries and payment
    in certain phase of the code I need to use the value choosed in the
    QComboBox "CB01" to window "Win2" in order to modify some data
    But I don't know how to transmit the value in "CB01" to the function
    "MSte()" in "Win2"
    but the way I'm using Qt4.8 with ubuntu 12.04
    and this is the code

    MainWin.cpp

    Qt Code:
    1. #include "MainWin.h"
    2. #include "Win1.h"
    3. #include "Win2.h"
    4. #include "Win3.h"
    5.  
    6.  
    7.  
    8. MainWin::MainWin()
    9. {
    10. //Societé
    11.  
    12. CB01=new QComboBox();
    13. QLabel *LAB01 = new QLabel("Societes :");
    14. QPushButton *PB01=new QPushButton("Ajouter");
    15. QPushButton *PB02=new QPushButton("Modifier");
    16. QPushButton *PB03=new QPushButton("Supprimer");
    17. HB01->addWidget(PB01);
    18. HB01->addWidget(PB02);
    19. HB01->addWidget(PB03);
    20. QPushButton *PB04=new QPushButton("Recapitulation");
    21. QVBoxLayout *VB01 = new QVBoxLayout;
    22. VB01->addWidget(LAB01);
    23. VB01->addWidget(CB01);
    24. VB01->addLayout(HB01);
    25. VB01->addWidget(PB04);
    26.  
    27. //Employés
    28.  
    29. CB02=new QComboBox;
    30. QLabel *LAB02 = new QLabel("Employes :");
    31. QPushButton *PB05=new QPushButton("Ajouter");
    32. QPushButton *PB06=new QPushButton("Modifier");
    33. QPushButton *PB07=new QPushButton("Supprimer");
    34. HB02->addWidget(PB05);
    35. HB02->addWidget(PB06);
    36. HB02->addWidget(PB07);
    37. QPushButton *PB08=new QPushButton("Paie");
    38. QVBoxLayout *VB02 = new QVBoxLayout;
    39. VB02->addWidget(LAB02);
    40. VB02->addWidget(CB02);
    41. VB02->addLayout(HB02);
    42. VB02->addWidget(PB08);
    43.  
    44. //Definition du titre de la fenetre
    45.  
    46. MW->addLayout(VB01);
    47. MW->addLayout(VB02);
    48. setLayout(MW);
    49. setWindowTitle("Easy Paie");
    50. resize(400,250);
    51.  
    52.  
    53. //Connextions des signaux et des slots
    54.  
    55. connect(PB01,SIGNAL(clicked()),this,SLOT(AddSte()));
    56. connect(PB02,SIGNAL(clicked()),this,SLOT(ModSte(CB01)));
    57. connect(PB03,SIGNAL(clicked()),this,SLOT(DelSte()));
    58. }
    59.  
    60. void MainWin::AddSte()
    61. {
    62. Win1 *win1=new Win1();
    63. win1->show();
    64. }
    65.  
    66. void MainWin::ModSte(QComboBox CB01)
    67. {
    68.  
    69. Win2 *win2(CB01)=new Win2();
    70. win2.show();
    71. }
    To copy to clipboard, switch view to plain text mode 

    MainWin.h

    Qt Code:
    1. #ifndef MAINWIN_H
    2. #define MAINWIN_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MainWin : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. MainWin();
    11. private slots:
    12. void AddSte();
    13. void ModSte(QComboBox CB01);
    14. void DelSte();
    15. private:
    16. QComboBox *CB01;
    17. QComboBox *CB02;
    18. QPushButton *PB01;
    19. QPushButton *PB02;
    20. QPushButton *PB03;
    21. QPushButton *PB04;
    22. QPushButton *PB05;
    23. QPushButton *PB06;
    24. QPushButton *PB07;
    25. QPushButton *PB08;
    26. };
    27.  
    28. #endif // MAINWIN_H
    To copy to clipboard, switch view to plain text mode 


    Win2.cpp


    Qt Code:
    1. #include "Win2.h"
    2. #include <fstream>
    3. #include <string>
    4. #include <QString>
    5.  
    6. using namespace std;
    7.  
    8.  
    9. Win2::Win2()
    10. {
    11. LE21=new QLineEdit;
    12. QLabel *LAB21 = new QLabel("Nom societe :");
    13. LE22=new QLineEdit;
    14. QLabel *LAB22=new QLabel("Num CNSS :");
    15. QPushButton *PB21=new QPushButton("Annuler");
    16. QPushButton *PB22=new QPushButton("Modifier");
    17. HB21->addWidget(PB21);
    18. HB21->addWidget(PB22);
    19. VB21->addWidget(LAB21);
    20. VB21->addWidget(LE21);
    21. VB21->addWidget(LAB22);
    22. VB21->addWidget(LE22);
    23. VB21->addLayout(HB21);
    24. setLayout(VB21);
    25. setWindowTitle("Modifier");
    26.  
    27. //connect
    28.  
    29. connect(PB21,SIGNAL(clicked()),this,SLOT(close()));
    30. connect(PB22,SIGNAL(clicked()),this,SLOT(MSte(CB01)));
    31.  
    32.  
    33.  
    34. }
    35.  
    36.  
    37. void Win2::MSte(QComboBox CB01)
    38. {
    39. string s1,s2,ss,ch;
    40. QString ste,cnss;
    41. ste=LE21->text();
    42. cnss=LE22->text();
    43. s1=ste.toStdString();
    44. s2=cnss.toStdString();
    45. ss=s2+' '+s1;
    46. ofstream NS("NSTE");
    47. ifstream S("STE");
    48. while(!S.eof())
    49. {
    50. S>>ch;
    51. if(ch==ss)
    52. NS<<"FUCK"<<endl;
    53. else
    54. NS<<ch<<endl;
    55. }
    56. S.close();
    57. NS.close();
    58.  
    59. close();
    60.  
    61.  
    62. }
    To copy to clipboard, switch view to plain text mode 


    Win2.h

    Qt Code:
    1. #ifndef WIN2_H
    2. #define WIN2_H
    3.  
    4. #include <QtGui>
    5.  
    6. class Win2: public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. Win2(QComboBox CB01);
    11. private slots:
    12. void MSte(QComboBox CB01);
    13.  
    14. private:
    15. QLineEdit *LE21;
    16. QLineEdit *LE22;
    17. QPushButton *PB21;
    18. QPushButton *PB22;
    19. };
    20.  
    21.  
    22. #endif // WIN2_H
    To copy to clipboard, switch view to plain text mode 




    And thank you

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: creating your own SLOT

    You access the current value of the combo box using QComboBox::currentText() and/or QComboBox::currentItem(). You expose that value to an external object by creating a public accessor function. This is standard C++.

    If you want the external object to know immediately that the combo box value has changed then you can use the one of the QComboBox::currentIndexChanged() signals to trigger the emission of a signal from the class containing the private combo box. You then arrange for that external signal to be connected to a slot in the other external object.

Similar Threads

  1. Replies: 2
    Last Post: 26th August 2011, 08:51
  2. Replies: 9
    Last Post: 13th August 2008, 18:07
  3. Problem When Creating my own Slot
    By Fatla in forum Qt Programming
    Replies: 12
    Last Post: 6th June 2008, 14:44
  4. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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
  •  
Qt is a trademark of The Qt Company.