This->hide(); or QMainWindow::hide(); not working



  • hello i got a little problem ...
    from mainwindow.cpp i call a function in login.cpp 'hidethis()' (the calling works - tested with qDebug)
    in this function in login.cpp 'hidethis()' there is a
    this->hide();
    or a
    QMainWindow::hide();

    to hide the login window
    but this does not work? i don't get it why ?

    i got
    login.cpp
    mainwindow.cpp
    login.h

    login.h:
    @
    ...
    class login : public QMainWindow{
    Q_OBJECT

    public:
    void hidethis();
    ...
    };
    @

    in mainwindow.cpp i got this in a function:

    @
    ...
    login login_win;
    login_win.hidethis();
    ...
    @

    in the login.cpp there is a function
    @
    void login::hidethis(){
    QMainWindow::hide(); // this is not working
    this->hide(); // this is also not working
    return;
    }
    @

    anyone any idea?
    thanks babu



  • @1. this->hide() refers to login object.

    1. QMainWindow::hide(); - This does not refer to any object.@

    You login object should have reference to main window object. From MainWindowObject, you should call hide.

    e.g

    @void login::hidethis(QMainWindow mainWin) {
    mainWin.hide();
    }

    You should call this function from MainWindow like this.

    loginobject.hidethis(this);
    @
    Not sure what logic you are trying. From simple programming perspective, you should do something like above.


  • Moderators

    @Dheerendra - that's not right. this refers to the login class instance which inherits QMainWindow. You don't need to pass it as a parameter. If you do pass it anyway you should use QMainWindow* or QMainWindow&, because in your example you create a copy, which is not possible with QObjects.

    This would simply work:
    @
    void login::hidethis() {
    hide(); //"this" is implicit, you don't need it
    //don't "return" from void function, it does nothing
    }
    @
    But it's really not necessary. You could just call hide() directly on the object. No need to create the hidethis() at all:
    @
    login login_win;
    login_win.hide();
    @
    But this doesn't make any sense really. When you create a widget it's not shown, so there's nothing to hide at this time.

    What do you mean it doesn't work? Do you mean it's shown? In that case you're not giving us some later code, because in what you pasted nothing shows the widget.



  • Thanks Chris. Yes login is inherited from QMainWindow. I missed it. After looking at logic::, I thought it is different class. It is my bac. Since it is it same class, passing this is definitely required. hide() can be directly called.



  • ok, i did not got this to work - i don't know why ?

    do you know what i am thinking wrong on this project?



  • ok i did the whole project new but i still got a problem

    first

    this works:
    login.cpp:
    @
    ...
    void login::on_pushButton_login_clicked(){
    hide();
    }
    @

    this does NOT work: (use of undeclared identifier 'hide')
    login.cpp
    @
    void hidethis(){
    hide();
    }
    @

    this also does NOT work: (out-of-line definition of 'hide this' does not match any declaration in 'login'
    @
    void login::hidethis(){
    hide();
    }
    @

    if i try to hide the login.ui from main window.cpp it also does NOT work

    what i tried:
    mainwindow.cpp:
    @
    login *login_win;
    login_win = new login();
    login_win->hide();
    @

    any idea why i cannot hide() the login form exept the first way ?

    what am i doing wrong?


  • Moderators

    Gosh, you really should start with some simple c++ before using Qt. These are fundamental c++ constructs. Maybe a book or a tutorial on classes first?
    I'm not trying to be harsh. It would make a lot of stuff easier for you as you're clearly struggling with basic concepts of what is a class, an instance, a pointer etc. and trying things until it works without understanding how and why it works. Get the basics right and it will all become trivial.

    @
    //this is a class method. works ok because
    //hide() in his context is a member method of QMainWindow,
    //which is the base class of login
    void login::on_pushButton_login_clicked(){
    hide();
    }

    //this is a free standing function unrelated to any class or instance.
    //hide() is meaningless in this context. Hide what? what is hide()?
    //there's no standing function named hide().
    void hidethis(){
    hide();
    }

    //are you misssing a declaration in the header?
    //if declared and called correctly this will work
    void login::hidethis(){
    hide();
    }

    //login_win is a local variable.
    //you are trying to hide a new window that is not even shown
    //this is NOT hiding the instance you are calling it from
    //also you're creating a memory leak by allocating local_win and never releasing it
    login *login_win;
    login_win = new login();
    login_win->hide();
    @



  • hi chris,
    i am new to QT and c++ but i thought programming something you are interested in is possibly the best way to learn it.
    sorry for getting on your nerves - will repeat the basic concepts again

    thanks for your explanation - i hope i will work it out


  • Moderators

    I'm not nervous, no worry. I just seem that way ;)

    Learning by doing is a great thing, but you don't start by building a car. First learn how an engine or the breaks work and build your way up. Otherwise you will just struggle a lot. It's hard to do the hard stuff without first knowing the easy stuff ;)

    First learn what's the difference between a method and a function, between a class and an instance, basics of OO like inheritance and polymorphism. What is a scope of a variable/member/function/method. Write some simple programs using these (without Qt).
    Then you can start with some basic concepts of Qt like event loop, signals/slots and ui. From there sky is the limit, but it will be easier and more natural to see how one thing is used to do another. If you jump at it and try to do everything at once it will just seem hard and chaotic and you will need to ask about every single thing, which you could easily figure out yourself or by reading docs otherwise.

    There's a c++ section on this forum if you need any help with these topics and there's a bunch of examples shipped with Qt.
    Good luck.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.