No idea how to manage QWidget



  • Hi Folks!

    I have two question, because I can't find the answer in documentation.

    First of all I created QWidget using Qt Wizard. In one word: normal windows application. Then I wanted to add new child window. I did it by writing it:

    @ QWidget *now = new QWidget();

    now->show();@
    

    but it's hard do design something by this way. So I created new window with Qt wizard and I added it into project. But now I have problem: how to show it, for example, after button clicked() singal?

    In Design section, on the right panel we can see all the properties of ours items. So, let's say we have only one basic window, and it's name is wnd. When I type:

    @wnd->close();@ nothing happens, compilator can't recognize the variable.

    So how I can manage QWidgets?



  • Via Qt Designer you create not widgets itself, but their description. You should instantiate them via new keyword. If you have created only ui file without c++ class than you can load this file via QUiLoader.



  • hmm...ok but when I created new whole project under class name "something" there are something.ui file (where I'm designing) and something.h which consist of:

    @#ifndef SOMETHING_H
    #define SOMETHING_H

    #include <QWidget>

    namespace Ui {
    class something;
    }

    class something: public QWidget
    {
    Q_OBJECT

    public:
    explicit something(QWidget *parent = 0);
    ~something();

    private:
    Ui::something*ui;

    private slots:
    void on_pushButton_clicked();
    };

    #endif // SOMETHING_H@

    So, according what you wrote I have class. How I can "do things" in "something" window by coding it, not designing?

    I tried

    @
    ui->something-> no help so I'm stuck
    @

    also:
    @
    ui->Ui_Something->...nothing
    @

    Any suggestions, or I'm doing it completely wrong?



  • Yes, you have a class. You can now instantiate it using
    @
    something somethingObj = new something;
    @
    and you will have this widget to show it. If you want to add it to another widget as its part you should pass this widget as parameter to constructor of something.

    You can access containtments of your designed widget by using ui pointer. For example if you have a label at your widget named myLabel you can access it using ui->myLabel.



  • bq. You can access containtments of your designed widget by using ui pointer. For example if you have a label at your widget named myLabel you can access it using ui->myLabel.bq.

    Yep, I know about this, but I created it like you said, and try basic things like:

    @
    something *somethingObj = new something;
    somethingObj->show();
    @
    after click, new, exactly the same windows appears.

    @
    something *somethingObj = new something;
    somethingObj->resize(10,13);
    @

    nothing happens? So I still don't know how to manage designed window by code of lines.

    Can you explain it to me again? Or recommend some documentation?



  • I think that you simply forgot to include show() in second sample of code, but used it in your sources. If not, try to add this.

    Also each widget has size hints and size policies (you can read about them in assistant), they defines how widget can be resized. Maybe you have minimum size for you widget equal to current size, so it cannot be resized smaller?



  • bq. I think that you simply forgot to include show() in second sample of code, but used it in your sources. If not, try to add this.bq.

    Yes! It's working.

    Sorry Denis but still one thing is nagging me all the time, and I'm can't find proper answer in net. I should start topic from this example:

    Imagine situation: I have parent widget which consist only lineedit and button. After button click new window(widget) should appear with another lineedit and button. User is typing something in lineedit, clicking button and then child widget is destroyed and it's passing variable to the basic widget and the first lineedit.

    So? How to do that? It will be something with "coinnect" and "slot" mechanism?


  • Moderators

    Luka: These questions of yours are pretty well covered in any introductory Qt book I have read so far. Maybe you could save some time by just grabbing a book and working through it? After all it must kill your productivity to sit and wait for answers here in the forum.

    Or you might try some of the free tutorials available on the net? Signals and slots are really well covered there as well.



  • yes, you're right Tobias, I was trying to follow the line of least resistance. My fault!


Log in to reply
 

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