Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] connect is not working

[solved] connect is not working

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matobodo
    wrote on last edited by
    #1

    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 happen

    Headers

    hlokno.h
    @#ifndef HLOKNO_H
    #define HLOKNO_H

    #include <QMainWindow>
    #include <QMdiArea>
    #include <QMenuBar>

    class hlOkno : public QMainWindow
    {
    Q_OBJECT

    public:
    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(&#41;;
    

    }
    @

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @
      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.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        matobodo
        wrote on last edited by
        #3

        i am just learning c++ and Qt and i don't understand from your reply how to solve it

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @
          Login *loginOkno = new Login(this);

          area->addSubWindow(loginOkno);
          @

          Or something similar.

          (Z(:^

          1 Reply Last reply
          0
          • M Offline
            M Offline
            matobodo
            wrote on last edited by
            #5

            @Login *loginOkno = new Login(this);

            area->addSubWindow(loginOkno);
            @

            line 3 error: no matching function for call to 'QMdiArea::addSubWindow(Login*&)'
            area->addSubWindow(loginOkno);
            ^

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Well, I don't remember the API right away. Try this:
              @
              area->addSubWindow(loginOkno->loginOkno);
              @

              (Z(:^

              1 Reply Last reply
              0
              • M Offline
                M Offline
                matobodo
                wrote on last edited by
                #7

                yes it works

                thank you

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved