Connecting a children widget with main widget
-
Hi!
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.
-
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.
-
Hi,
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
@void PanelDibujo::linea()
{
lineaMarked = true;
}@But nothing happens...
It works with SLOT-methods like hide() etc... -
[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:
void linea();@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!