Seite 1 von 1
aus slot konstruktor aufrufen
Verfasst: 29. September 2004 13:41
von volture
hallo,
ich möchte aus einer slotroutine einen konstruktor aufrufen und dabei einen integer wert übergeben. kann mir da jemand helfen.
volture
Verfasst: 29. September 2004 17:08
von lepsai
was heißt "einen Konstruktor aufrufen"? Was willste denn konkret machen?
Verfasst: 30. September 2004 09:26
von volture
also ich möchte aus dem slot:
void mainWindow::Slot(int a){
}
den konstruktor aufrufen:
Dialog::Dialog( QWidget* parent, const char* name)
: dialog( parent, name)
{
}
und dabei den parameter a übergeben.
Verfasst: 30. September 2004 09:41
von lepsai
na dann mach doch:
void mainWindow::Slot(int a)
{
static Dialog * pDlg = new Dialog(this, "dialog");
pDlg->exec();
}
wo ist denn nun das Problem?
Verfasst: 30. September 2004 09:51
von volture
aber ich kann immer noch nicht im konstruktor auf die variable a zugreifen.
Verfasst: 30. September 2004 10:08
von lepsai
Dialog ist ja deine Klasse, also kannst du eine Funktion schreiben setA(int a) und diese in deinem slot aufrufen...
Verfasst: 30. September 2004 11:41
von volture
also irgendwie bekomme ich das nicht hin. schein zu dumm zu sein.
also:
meine ..ui.h
void mainWindow::Slot(int a)
{
Dialog aufruf;
aufruf.setA(a);
Dialog * pDlg = new Dialog(this, "dialog");
pDlg->exec();
}
meine..h
class Dialog : public dialog
{
Q_OBJECT
private:
int k;
public:
Dialog( QWidget* parent = 0, const char* name = 0);
~Dialog();
setA(int a)
{
k=a;
};
};
meine ...cpp
Dialog::Dialog( QWidget* parent, const char* name)
: dialog( parent, name)
{
test->setNum(k);
}
die ausgabe der variable k stimmt nicht mit der ursprungsvariable a überein.
kann mir jemand den fehler sagen.
danke volture
Verfasst: 30. September 2004 13:22
von monte
volture hat geschrieben:
void mainWindow::Slot(int a)
{
// Dialog aufruf;
// aufruf.setA(a);
Dialog * pDlg = new Dialog(this, "dialog");
pDlg->setA( a );
pDlg->exec();
}
Der Konstrucktor wird ja immer als erstes aufgerufen also vor der Funktion
setA( a ); daher kann man meiner meinung nach so im Konstrucktor
auch nicht auf a zugreifen ( es ist fraglich ob man das überhaupt braucht )
man könnte allerdings den Konstrucktor um eine Variable a erweitern
Dialog * pDlg = new Dialog(this, "dialog", a);
pDlg->setA( a );
pDlg->exec();
----------- Schnipp ------------------
private:
int k;
public:
Dialog( QWidget* parent = 0, const char* name = 0, int a = 0 );
~Dialog();
------------- Schnapp ---------------------------------------
-------------- Schnipp ---------------------
Dialog::Dialog( QWidget* parent, const char* name, int a )
: dialog( parent, name), k(a)
{
test->setNum(k);
}
------------ Schnapp ---------------------------
Verfasst: 30. September 2004 14:41
von volture
die konstruktorerweiterung um a hat perfekt geklappt.
dank dir!
volture