October 7, 2011

toho71 toho71
Lab Rat
189 posts

[SOLVED]Get mainwindows Widgets

 

I’m building a desktop application and from a widget that i placed in another widget, reach a label on the mainwindow to change text or color or something.

I’ve tried to send the ref from thet labelwidget thru the constructor but i cant do anything with it.

Is it the parentwidget etc etc or am I completly wrong.

8 replies

October 7, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

Not clear what you want to achieve, but QWidget has a method to get its parent. However I don’t see the problem on keeping a pointer/reference to the widget somewhere, e.g., in your main window class.

Also QObject::children() [doc.qt.nokia.com] could be helpful for you.

[EDIT: merged two comments, please edit comments, do not double comment, Volker]

October 7, 2011

toho71 toho71
Lab Rat
189 posts

AFter i pressed a button on my shown widget i want to change the state of a label in the Mainclass.

And i tried to do this just like I do in Java but I got alot of errors in that case

October 7, 2011

Volker Volker
Robot Herder
5428 posts

You should NOT change the label from the button class. Use signal/slots mechanism to notify the main widget class of the requested change. The buttons provide a clicked() signal that you can connect to.

October 7, 2011

toho71 toho71
Lab Rat
189 posts

So this code is Compleatly wrong

Just a little pseudo

On the widgets Button click()
mainwindow->Label->setText(“Text I want to setSet”)

October 7, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

Steps are the following:

  • connect a signal from your button to a slot of yours
    1. connect( myButton, SIGNAL(clicked()), this, SLOT(updateLabelSlot()));
  • in the slot perform the update of the text
    1. void MainWindow::updateLabelSlot(){
    2.    Label->setText("whatever you want");
    3. }

October 7, 2011

toho71 toho71
Lab Rat
189 posts

I’m very bad to explain my problem I think.
Bad english

My case

I got a mainwindow with some widgets .
Qlabel Qtab etc etc

On every tab i have diffrent widgets(Qtforms and Classes)
i Instance the Class when i open that tab
and show that widget.

for exampel
On one widget there are ex . 6 buttons with diffrent conditions and when i click a button i want the text on the Label on the mainwindow to append or change the text.

Is the post above the rigth way to handel this case?

I’ll give it a try.
//Thomas

October 7, 2011

Volker Volker
Robot Herder
5428 posts

Hi Thomas,

in that case you would connect the buttons’ clicked() signals to on or more internal slot(s) of your widget. In that you emit a newly defined signal, e.g. changeLabelText(QString). You then connect that signal to the main window’s label.

eg.

  1. // --- in the header file for your widgets in the tab
  2. class MyWidget : public QWidget
  3. {
  4.    Q_OBJECT
  5.  
  6. public:
  7.    // constructors etc.
  8.  
  9. signals:
  10.    void changeLabelText(const QString &text)
  11.  
  12. protected slots:
  13.    void onButton1Clicked();
  14.  
  15. };
  16.  
  17. // --- in the cpp file for your widgets in the tab
  18.  
  19. MyWidget::MyWidget(QWidget *parent)
  20.   : QWidget(parent)
  21. {
  22.     // setup your usual stuff here
  23.     connect(ui->button1, SIGNAL(clicked()), this, SLOT(onButton1Clicked()));
  24. }
  25.  
  26. MyWidget::onButton1Clicked()
  27. {
  28.     emit changeLabelText("I have clicked on button 1!");
  29. }
  30.  
  31. // --- in your main widget, where you setup or insert the widgets:
  32.  
  33. MainWidget::insertTab(MyWidget *widget)
  34. {
  35.     tabWidet->addTab(widget, "my widget");
  36.     connect(widget, SIGNAL(changeLabelText(QString), label, SLOT(setText(QString)));
  37. }

You can ease your life, if you create a base class MyTabContentWidget which does less more than define the signal changeLabelText and subclass your actual widgets from that. This way you have a nice interface to use in your main widget.

October 7, 2011

toho71 toho71
Lab Rat
189 posts
Nice. I’ll try this. { It works perfectly } Thanks to all
 
  ‹‹ problem in making Fixed size QtabWidget’s tab      how we can set enter key to a widget so that while pressing enter a click event should occur? ››

You must log in to post a reply. Not a member yet? Register here!