Display a new settings ui for my widget.



  • I have a widget that when run will display a form and some data.
    I wanted to add another ui to that in order for someone to be able to adjust some settings.
    I instantiate that second ui as such

    @// settings
    QWidget *widget = new QWidget;
    Ui::FormWidget ui;
    ui.setupUi(widget);
    widget->show();@

    But this way though I cant seem to be able to connect with the signals of that ui.
    So even if I have on_SomeButton_clicked() when I click the button nothing happens.
    Is there any way to fix this and be able to connect to my second forms signals.


  • Lifetime Qt Champion

    Hi,

    widget doesn't have any of these slot so it won't work like that. When creating FormWidget didn't you also got that widget created ?



  • No I created a dialog using Designer that contains all the text edits and buttons of my form, and I was hoping to use that dialog from my existing app, hoping to avoid having to deal with with another layer that will communicate with the dialog and pass the data to my app.



  • i'm not sure to well understand, but you can use your ui for other dialog box... you can also override your dialogbox class and some member functions to use it too...
    but... a dialog box is not a widget (and a widget is not a dialog box too).

    could you explain mor ein details please (or maybe someone understand better than me what you would like to do) ?



  • Sure, I can try. I created a custom widget that what it does is simply paint a rectangle, move it across the screen at some speed and takes care any collisions with the window edge, imagine something like a small screensaver if you will.
    What I thought is that it would be nice if I created some sort of a dialog that will pop up, and give you the ability to define some option like the colour and speed for that rectangle. So if you change something on that 'settings dialog that will pop up' the change will also reflect on the actual rectangle.
    To do that I just created a new dialog using the Designer, added the elements that I wanted and hoped I'd be able to instantiate and use it from inside the class that defines my custom widget (the project has a main.cpp a screensaver.cpp and .h and the .ui file I created)



  • i don't know wivh code style you use too... QML or C++ ? I think C++ because of QWidget... right ?
    Also, you can probably do it by mount existing events from your custom widget (in fact, from the content signals contained by this widget) and emit a signal from this event to link with a slot. This signal you have to emit could pas the args you need to the slot for do the job and open a dialog box by fucntion "open()". You need to learn about signal/slot for do that.

    so you have to decide when your dialog box has to be open... by a key combinaison, by a event mouse click, by an other event from your custom widget... then use this event and emit a signal, create a slot and connect them together (don't forget to do a qmake and incule Q_OBJECT for see the effect).
    Then inside your slot (mybe isnide your main part), do the job to define the dialog_box pointer and open it... at result time back (or if you want do it before close the dialog box, you can also use events from widgets are inside this dialog box...) you can do the job with the content from your dialog box widgets entries...

    this is an idea... maybe there is some better one, i'm not an expert and i think some other here would be better for give you the best solution (Sgaist or Andre for exemple).



  • I see your point, and thanks for the suggestion, but I am more interested in knowing why a signal from a ui file works only with the class that is automatically created by qt.
    If I just want to make a guy with Designer, how can I use it if it will only work with one particular class (even if I do the exact steps to instantiate the gui from my class as the ones that Qt does).
    When I was learning about scripting I did come across CustomUiLoader, but I haven't seen Qt using that when it creates a new ui with a class.


  • Lifetime Qt Champion

    You can do it from somewhere else but then it's up to you to do the connections or write the slots in your custom widget with the correct naming scheme to allow automatic connection.



  • I did add the slots in my own widget with the correct naming

    @MyWidget::on_pushButtonName_clicked()
    {
    qDebug() << "test";
    }@

    if that is what you mean.

    I never saw "test" being displayed in my console though.



  • ealione, this not works like that.
    this simple slot you use is ok for what you tell about connection from ui only mounted by the class use the ui (so like you see by yourself, this is limited).
    BUT
    yuo can CREATE a signal yourself emit from an event, and then everywhere you decide (out of your class), you can connect the signal to a slot.

    SO... for do it you need:

    declare a signal in the header file (under the "private signal:" declaration mark)

    define this signal emition by an "emit" follow by the name of the signal and his args (in this situation, directly from your event on_pushButtonName_clicked() if you need... but you can emit from every wher you need inside this class who emit from your ui...)

    go in the class you want and declare a slot in the header file (under the mark: "private slot:")

    in your source file of the class you have to define this slot (same that define a member function)

    you need also to connect the signal with the slot by a "QObject::connect(.......)" and look at the doc for learn how to do it well

    link it by a qmake (from your menu bar compil inside QtCreator)

    and... voila.

    if you not do this, sure you will never see nothing appears.
    it is not to much easy, but it is not to much complicate.
    also, if you do all of that and find more difficulties (due for exemple of a bad understand of the doc... because for sure, the doc, when we are beginner is not to much easy to understand... i'm a geginner and i had the excatly same problem than you....), then you can pastbin your code for we can have a look and resolve your problem by coorect the code and explain where the problem come from.

    this will works for you if you just follow the doc about: "SIGNAL / SLOT"
    and some help we try to provide.



  • Thank you for your answer jerome, I'll give this a try.


Log in to reply
 

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