How to access objects of parent Dialog from Child Dialog .



  • Hello respected Qt experts and my dear forum friends,
    I have a doubt ,Suppose I in my first dialog.ui i have dragged a label through designer and in my second dialog.ui ,I have a radio button called setlabel and a pushbutton called ok,so when i click on radiobutton and click on ok pushbutton,the label should change in my first dialog.ui...so how to do it..using signals and slots..

    regards
    imrrk



  • Hi imrrk,

    You already answered your question...do it by using "Signals and slots":http://doc.qt.nokia.com/latest/signalsandslots.html :).
    It is really easy to connect the signals from your radio and push button with the label's slot.

    regards,
    wladek



  • hm, you cant connect directly in designer, as they are different dialogs.

    you need a signal in your dialog (typically in your custom implementation) and a slot in the other one (also in the custom implementation) and connect them.



  • imrrk, as you have been told in your calendar thread: the best way is to make sure that you have a clear interface between different objects in your application. That is: don't try to reference (or connect to) internals of one object from another one. Instead, expose the parts you need to expose at the interface of the classes. In this case, that means that your first class (the one with the label) gets a slot setLabel(const QString& text), and that the second class (with the button) gets a corresponding signal. You connect the two at a place where you have references to both the objects. This way, it doesn't matter what happens internally in either class, and you can change that however you like. You can replace the label by some other kind of widget, or you can change the kind of UI you choose for changing it. However, the programming interface: the public slot and signal in the classes's interfaces, can remain the same.



  • hey gerolf,can u explain me with my example so it will be very clear for me,and I have rightclicked the project and added the new designer form class from which i got dialog1.ui,dialog1.cpp and dialog1.h,so if you u explain with my example above ,i will be very thankful to you..

    regards
    imrrk



  • hey andre,I am confused,please explain me with short code snippet..

    regards
    imrrk



  • imrrk,

    perhaps you should try to get one of the really good books from the books page of the "wiki":http://developer.qt.nokia.com/wiki/Books_and_Links_for_learning_C_and_advanced_topics and read it. How to make signals and slots and how to make dialogs is basic Qt.

    I suggest this one: Summerfield et.al: C++ GUI Programming with Qt 4

    It explains many of your questions here on dev net and is really god.



  • Hey i understood the concept...i am clear now.thanks alot



  • hello ,if I have written same signals for two differnt slots

    @
    connect(ui->calendarWidget,SIGNAL(clicked(QDate)),this,SLOT(showdate(QDate)));
    connect(ui->calendarWidget,SIGNAL(clicked(QDate)),this,SLOT(showset(QDate)));
    @

    and from the theory it is written that it will that slots will be called in order in which they are called,but my case they are getting called at once..so whether i have gone wrong?

    [EDIT: code formatting, Volker]
    regards
    imrrk



  • Your understanding of what is going on. They are not called at the same time.



  • but when i click on calendar widget the slots are getting called at once only



  • No, they are NOT. They are called consecutively. First the one, then the other. Why would you think they are called at the same time?



  • hello andre,I have taken a calendar widget and text browser in my first dialog,so on click(Qdate )signal ,i have written a slot for showing the text on browser,then again on click(Qdate) I have written one more slot where it opens a new dialog..so when a user clicks for the firsttime a date is displayed in textbrowser,but when he clicks the second time he is directed to new dialog,but in my case both conditions are happening at the same click and not on 2 clicks,

    regards
    imrrk



  • That is how it should be. You made two connections. Each of the two connected slots are executed for each time the signal is fired. That means that on the first click, you will get both slots executed (in order), and on the second click they will both get executed again.



  • so wats the solution?,but if i want the dialog to be opened only on second click then what we should do



  • Re-think your UI so that performing an action on a date will result in the same effect the first time as it does the second time, for instance. I find it weird that you want to display one dialog the one time, and another one the second time. What would happen the third time? Or the tenth?

    That is: unless your dates display some kind of visual state to differentiate them. But then, that is something you can use for differentiating between the actions you want to perform as well.



  • if we look at std phone calendar,onfirstclick they just show the date,but if we again click the date ,a dialog is opened and we are asked to set reminders,I am asking u that kind of mechanism..

    regards
    imrrk



  • So, that means that you only do something with a click, if that date is already the selected date. Well... you have access to the selected date, so I suggest you use it.



  • ok can u show me by a small code snippet..it will be very helpful

    regards
    imrrk



  • hey whether i should use selectedDate function..?



  • hello friends,I have a doubt,Now when i click on any date in calendar widget,I have written slot so as to show the date in a textbrowser as shown in code below
    @void Dialog::on_Date_Clicked(QDate date)
    {
    ui->TextBrowser->setText(date.toString());

    }@
    Now again when i click on selected date I want to open a newdialog
    so on first click it shows the date in browser and on secondclick it should open a dialog..
    so how should i proceed..

    regards
    imrrk



  • Create a state machine and handle it there :-)

    if you need different actions on the same signals, reconnect to different slot, have some state variables, whatever.



  • [quote author="imrrk" date="1302784064"]hello friends,I have a doubt,Now when i click on any date in calendar widget,I have written slot so as to show the date in a textbrowser as shown in code below
    @void Dialog::on_Date_Clicked(QDate date)
    {
    ui->TextBrowser->setText(date.toString());

    }@
    Now again when i click on selected date I want to open a newdialog
    so on first click it shows the date in browser and on secondclick it should open a dialog..
    so how should i proceed..

    regards
    imrrk[/quote]

    Well, one way might be to compare the current content of the text browser with the string representation of the date that was clicked. If they match, then you open the dialog, if they don't, you only set the textual representation of the date in the text browser. Or one of the many, many other ways (most of them better structured than this one) you can do this.

    imrrk, this is reall, really basic programming. I would recommend you do some kind of basic programming course, or at least get yourself a good book on the topic at beginners level.



  • hey i know andre,and i can do it,but in I am not understanding it in qt framework,so I asked it..



  • hey gerolf,I connected same signal to different slots,but both slots gets called on single click ,but i want that the on first click first slot should get called and on second click second slot..



  • [quote author="imrrk" date="1302951509"]hey i know andre,and i can do it,but in I am not understanding it in qt framework,so I asked it..
    [/quote]

    The reason why I recommend you get yourself some basic knowledge, is that this question has nothing to do with Qt itself. You will run into the exact same issue in any framework and in any language. That's why I think you lack some basic knowledge to be an effective programmer.



  • ok andre,thanks for your advice,hey i i have tried something,just check out my code,
    @#include "dialog.h"
    #include "ui_dialog.h"
    #include<QCalendarWidget>
    #include<QDate>
    #include "dialog1.h"
    #include "ui_dialog1.h"
    #include<QPainter>
    #include <QtGui/QApplication>

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    connect(ui->calendarWidget,SIGNAL(clicked(QDate)),this,SLOT(showdate(QDate)));
    

    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::showdate(QDate date)
    {
    ui->lineEdit->setText(date.toString());

    }
    

    void Dialog::showset(QDate date)
    {

    if(ui->lineEdit->text()==date.toString())
    {
    Dialog1 a(this);
    connect(&a,SIGNAL(setclicked()),this,SLOT(setcolors()));
    connect(&a,SIGNAL(unsetclicked()),this,SLOT(unsetcolors()));
    a.show();
    a.exec();

    }
    else
    {
    ui->lineEdit->setText("hello");
    }

    }@

    regards
    imrrk



  • I will be thankful if anyone check out this code and tell me ..



  • imrrk, if you show us a piece of code, without telling us what to look for, do you really expect a reply? My guess is: it doesn't work as you expect, right?

    Just a hint: you implemented a showset(QDate date) method, but you are not calling it anywhere. I think you should merge the showdate and showset methods into a single method.



  • why using:
    a.show and a.exec?



  • hello andre,u already know what i want,so i showed my code,to show you what i have tried,and you r right ,i forgot to include that line,where i called showset method..



  • vinb,I have used that to show and execute the dialog



  • [quote author="imrrk" date="1303117850"]hello andre,u already know what i want,so i showed my code,to show you what i have tried,and you r right ,i forgot to include that line,where i called showset method..[/quote]

    Yes, it is clear what you want, but not what might be wrong with the code you show us. That is what I was asking about. I guess it doesn't do what you want, but that's not what you tell us.

    On the show() and exec() calls. Just remove the show() call please, it is nonsense.



  • ok andre,I removed it,what next?



  • Do you have introduced a call to showset(QDate date) yet? Or better yet, did you follow my advice on merging that method with showdate(QDate date)?



  • i have introduced the call in constructor but also it wont work and do you mean merging in this way

    @void Dialog::showdate(QDate date)
    {
    ui->lineEdit->setText(date.toString());
    connect(ui->calendarWidget,SIGNAL(activated(QDate)),this,SLOT(showset(QDate)));

    }
    void Dialog::showset(QDate date)
    {
    Dialog1 a(this);

    connect(&a,SIGNAL(setclicked()),this,SLOT(setcolors()));
    connect(&a,SIGNAL(unsetclicked()),this,SLOT(unsetcolors()));
    a.showNormal();
    a.exec();
    }@



  • No, no, no!

    Now you are adding a new connection on each invokation of showdate. You only want one connection between a signal and a slot.

    Your previous version of showset was almost correct. It looked like this:
    @
    //re-formatted for better readability

    void Dialog::showset(QDate date)
    {
    if(ui->lineEdit->text()==date.toString()) {
    Dialog1 a(this);
    connect(&a,SIGNAL(setclicked()),this,SLOT(setcolors()));
    connect(&a,SIGNAL(unsetclicked()),this,SLOT(unsetcolors()));
    // a.show(); <--- commented out, not needed.
    a.exec();
    } else {
    ui->lineEdit->setText("hello");
    }
    }
    @

    That was quite close. There is an error in your logic here though. Explore when the else branch will be executed. Only if the date in the lineEdit is not equal to the current date. In response, you then set it to a placeholder text again. So, the next time, this method is executed, it will execute the else branch again.

    What you would need to do, is remove* this line that sets the hello text. Replace it by a line that sets the current date as the string (line 3 from your last code snippet). Also remove the showDate() method completely (and remove the connect statement that makes a connection to this now removed slot), and in the constructor of your class create a connection like this:

    @
    connect(ui->calendarWidget,SIGNAL(clicked(QDate)),this,SLOT(showset(QDate)));
    @

    *) By removing, you get the behaviour that the first time a specific date is clicked, only the text in the line edit is set to the date you just clicked, but all the next times it is clicked the dialog will be shown, until you click another date and the process begins from the start. If you really want to show the dialog only on each second click, you need to re-set the line edit after each time you have shown the dialog, so in the first branch of the if statement.



  • thank you very much andre,its working fine...

    cheers


Log in to reply
 

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