[solved] connect is not working
-
i am trying to connect pushbutton with slot loginTry() in file login.cpp line 37
connect return true but when i push the button nothing happenHeaders
hlokno.h
@#ifndef HLOKNO_H
#define HLOKNO_H#include <QMainWindow>
#include <QMdiArea>
#include <QMenuBar>class hlOkno : public QMainWindow
{
Q_OBJECTpublic:
hlOkno(QWidget *parent = 0);
~hlOkno();protected:
QMenuBar *menu;
QMdiArea *area;void createMenu();
};
#endif // HLOKNO_H
@login.h
@#ifndef LOGIN_H
#define LOGIN_H#include <QObject>
#include <QLabel>
#include <QComboBox>
#include <QLineEdit>
#include <QPushButton>
#include <QMdiSubWindow>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QCryptographicHash>class Login : public QObject
{
Q_OBJECT
public:
explicit Login(QObject *parent = 0);void nastavPrihlasovanie(); QMdiSubWindow *loginOkno;
protected:
QLabel *menoLbl, *hesloLbl;
QComboBox *meno;
QLineEdit *heslo;
QPushButton *login, *koniec;
QHBoxLayout *layoutBtn;
QGridLayout *layoutLblInput;
QVBoxLayout *layoutCentral;QStringList uzivatelia, hesla;
signals:
private slots:
void loginTry();};
#endif // LOGIN_H
@Sources
hlokno.cpp
@#include "hlokno.h"#include "login.h"
hlOkno::hlOkno(QWidget *parent)
: QMainWindow(parent)
{
menu = new QMenuBar(this);
area = new QMdiArea(this);setCentralWidget(area); setMenuBar(menu); createMenu(); Login loginOkno; area->addSubWindow(loginOkno.loginOkno);
}
hlOkno::~hlOkno()
{}
void hlOkno::createMenu()
{
//set menu buttons here
}
@login.cpp
@#include "login.h"#include <QDebug>
Login::Login(QObject *parent) :
QObject(parent)
{
loginOkno = new QMdiSubWindow;menoLbl = new QLabel("Jméno:",loginOkno); hesloLbl = new QLabel("Heslo:",loginOkno); meno = new QComboBox; heslo = new QLineEdit; login = new QPushButton("Prihlásit",loginOkno); koniec = new QPushButton("Konec",loginOkno); layoutLblInput = new QGridLayout; layoutBtn = new QHBoxLayout; layoutCentral = new QVBoxLayout; layoutLblInput->addWidget(menoLbl,0,0); layoutLblInput->addWidget(hesloLbl,0,1); layoutLblInput->addWidget(meno,1,0); layoutLblInput->addWidget(heslo,1,1); layoutBtn->addWidget(login); layoutBtn->addWidget(koniec); layoutCentral->addItem(layoutLblInput); layoutCentral->addItem(layoutBtn); delete loginOkno->layout(); loginOkno->setLayout(layoutCentral); nastavPrihlasovanie(); bool r = connect(login,SIGNAL(clicked()),this,SLOT(loginTry())); //******************* here the slot not work qDebug() << r;
}
void Login::nastavPrihlasovanie()
{
uzivatelia << "admin" << "user1" << "user2";
hesla << QString(QCryptographicHash::hash("admin",QCryptographicHash::Md5).toHex());
hesla << QString(QCryptographicHash::hash("user1",QCryptographicHash::Md5).toHex());
hesla << QString(QCryptographicHash::hash("user2",QCryptographicHash::Md5).toHex());meno->addItems(uzivatelia);
}
void Login::loginTry()
{
qDebug() << "Button clicked! :)";
}
@main.cpp
@#include "hlokno.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
hlOkno w;
w.show();return a.exec();
}
@ -
@
Login loginOkno;area->addSubWindow(loginOkno.loginOkno);
@You are creating a local variable, on the stack, in the constructor of hlOkno. That variable is destroyed when the constructor finishes. You should create it on heap instead (with "new"). I am surprised you see the interface at all.
-
@
Login *loginOkno = new Login(this);area->addSubWindow(loginOkno);
@Or something similar.
-
Well, I don't remember the API right away. Try this:
@
area->addSubWindow(loginOkno->loginOkno);
@