PDA

View Full Version : creating your own SLOT



the9gamer
31st July 2012, 21:10
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


#include "MainWin.h"
#include "Win1.h"
#include "Win2.h"
#include "Win3.h"



MainWin::MainWin()
{
//Societé

CB01=new QComboBox();
QLabel *LAB01 = new QLabel("Societes :");
QPushButton *PB01=new QPushButton("Ajouter");
QPushButton *PB02=new QPushButton("Modifier");
QPushButton *PB03=new QPushButton("Supprimer");
QHBoxLayout *HB01=new QHBoxLayout;
HB01->addWidget(PB01);
HB01->addWidget(PB02);
HB01->addWidget(PB03);
QPushButton *PB04=new QPushButton("Recapitulation");
QVBoxLayout *VB01 = new QVBoxLayout;
VB01->addWidget(LAB01);
VB01->addWidget(CB01);
VB01->addLayout(HB01);
VB01->addWidget(PB04);

//Employés

CB02=new QComboBox;
QLabel *LAB02 = new QLabel("Employes :");
QPushButton *PB05=new QPushButton("Ajouter");
QPushButton *PB06=new QPushButton("Modifier");
QPushButton *PB07=new QPushButton("Supprimer");
QHBoxLayout *HB02=new QHBoxLayout;
HB02->addWidget(PB05);
HB02->addWidget(PB06);
HB02->addWidget(PB07);
QPushButton *PB08=new QPushButton("Paie");
QVBoxLayout *VB02 = new QVBoxLayout;
VB02->addWidget(LAB02);
VB02->addWidget(CB02);
VB02->addLayout(HB02);
VB02->addWidget(PB08);

//Definition du titre de la fenetre

QVBoxLayout *MW = new QVBoxLayout;
MW->addLayout(VB01);
MW->addLayout(VB02);
setLayout(MW);
setWindowTitle("Easy Paie");
resize(400,250);


//Connextions des signaux et des slots

connect(PB01,SIGNAL(clicked()),this,SLOT(AddSte()) );
connect(PB02,SIGNAL(clicked()),this,SLOT(ModSte(CB 01)));
connect(PB03,SIGNAL(clicked()),this,SLOT(DelSte()) );
}

void MainWin::AddSte()
{
Win1 *win1=new Win1();
win1->show();
}

void MainWin::ModSte(QComboBox CB01)
{

Win2 *win2(CB01)=new Win2();
win2.show();
}




MainWin.h


#ifndef MAINWIN_H
#define MAINWIN_H

#include <QtGui>

class MainWin : public QWidget
{
Q_OBJECT
public:
MainWin();
private slots:
void AddSte();
void ModSte(QComboBox CB01);
void DelSte();
private:
QComboBox *CB01;
QComboBox *CB02;
QPushButton *PB01;
QPushButton *PB02;
QPushButton *PB03;
QPushButton *PB04;
QPushButton *PB05;
QPushButton *PB06;
QPushButton *PB07;
QPushButton *PB08;
};

#endif // MAINWIN_H



Win2.cpp



#include "Win2.h"
#include <fstream>
#include <string>
#include <QString>

using namespace std;


Win2::Win2()
{
LE21=new QLineEdit;
QLabel *LAB21 = new QLabel("Nom societe :");
LE22=new QLineEdit;
QLabel *LAB22=new QLabel("Num CNSS :");
QPushButton *PB21=new QPushButton("Annuler");
QPushButton *PB22=new QPushButton("Modifier");
QHBoxLayout *HB21=new QHBoxLayout;
HB21->addWidget(PB21);
HB21->addWidget(PB22);
QVBoxLayout *VB21=new QVBoxLayout;
VB21->addWidget(LAB21);
VB21->addWidget(LE21);
VB21->addWidget(LAB22);
VB21->addWidget(LE22);
VB21->addLayout(HB21);
setLayout(VB21);
setWindowTitle("Modifier");

//connect

connect(PB21,SIGNAL(clicked()),this,SLOT(close())) ;
connect(PB22,SIGNAL(clicked()),this,SLOT(MSte(CB01 )));



}


void Win2::MSte(QComboBox CB01)
{
string s1,s2,ss,ch;
QString ste,cnss;
ste=LE21->text();
cnss=LE22->text();
s1=ste.toStdString();
s2=cnss.toStdString();
ss=s2+' '+s1;
ofstream NS("NSTE");
ifstream S("STE");
while(!S.eof())
{
S>>ch;
if(ch==ss)
NS<<"FUCK"<<endl;
else
NS<<ch<<endl;
}
S.close();
NS.close();

close();


}



Win2.h


#ifndef WIN2_H
#define WIN2_H

#include <QtGui>

class Win2: public QWidget
{
Q_OBJECT
public:
Win2(QComboBox CB01);
private slots:
void MSte(QComboBox CB01);

private:
QLineEdit *LE21;
QLineEdit *LE22;
QPushButton *PB21;
QPushButton *PB22;
};


#endif // WIN2_H






And thank you
:cool:

ChrisW67
1st August 2012, 00:39
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.