Connecting a children widget with main widget
I am a Qt-newbie, and need some help. I have to make a kind of simple Painter, and I have a one main widget where I have menubar and 2 buttons. One for draw a line, and one for rectangles. The problem I have, is how to connect those two actions with the happenings in the children widget.
I thought first that I can do a slot-method that return some kind of boolean, and in the canvaswigdet class make a check if its drawline or drawrectangle. But that didn't work.
Can somebody help me?
I used QtCreator with QtDesigner for the GUI.
Your idea to use signals and slots is a good one. That is the way to deal with button clicks. However, slot-methods usually do not return a value. They are usually declared void, and only take arguments. What you are after, I think, is some kind of drawing mode selection. Look into QButtonGroup to make a set of exclusive (tool) buttons. In your drawing code, you check in which mode you are. It depends a bit where your drawing code is located (in the main widget code, or in your canvas widget code) how you should communicate this state.
Or just define two slots drawLine() and drawRect() that you connect the respective buttons to.
Can I have define those slots in the prototype of mainwindow and access them from the children widget?
Children widget? What widget would that be?
What do you mean by "access"? Call them? Sure, if your "Children widget" has a reference to the mainwindow and the slots are active, you can call them. But I doubt that is what you are after.
If your "children widget" is the widget responsible for the drawing, you probably want to define the slot(s) on that widget directly. Then, you can connect the signal from your buttons (or button group) to that slot or those slots from the mainwidget's constructor.
Yeah, children widget is the canvas widget.
So I should define the slots directly there. Will not be a problem even the actions come from the MainWindow_ui?
You define the slots in the class that will do the drawing.
You only can define slots in UI components that have a C++ class on its own, not on widgets that are just child widgets of some other UI.
I am colloborating with blixs. Thanks for your answers, wanted to make it a bit clearer...
This is in our ui_mainwindow.h
@ void setupUi(QMainWindow *MainWindow)
actionLinea = new QAction(MainWindow);
centralWidget = new PanelDibujo(MainWindow);
QObject::connect(actionLinea, SIGNAL(hovered()), centralWidget, SLOT(linea()));
And should trigger in our childwidget paneldibujo.cpp
lineaMarked = true;
But nothing happens...
It works with SLOT-methods like hide() etc...
is linea() declared as slot?
Did you check debugging output if there are errors / warnings?
No, it is not declared as a slot. You need to do that somewhere?
What does the console output show? Any errors about connections?
It should work too, if you declare linea() with the "public/protected/private slots" pseudo keyword.
[quote author="tekblom" date="1295269403"]No, it is not declared as a slot. You need to do that somewhere?[/quote]
Yes, you must add that in the class definition (usually in a .h file). See the "Signals & Slots":http://doc.qt.nokia.com/stable/signalsandslots.html docs for some examples. Unfortunately the docs say it not clear enough, that you must add the "public/protected/private slots" keywords.
Oh, yeah I found it now.
Object::connect: No such slot PanelDibujo::linea() in ./ui_mainwindow.h:109
Object::connect: (sender name: 'actionLinea')
Object::connect: (receiver name: 'centralWidget')
I added in paneldibujo.h
@ public slots:
and the error disappeared but still nothings happens. Use the same code as before...
But now...Wow it works ;)
Had to rebuild it some times!
Thanks for the help!
You should at least call qmake after you add the first signal or slot to your class to catch up the changes.