tags:

views:

114

answers:

2
+1  Q: 

qt GUI connecting

I am just starting out with QT. I have read through some tutorials, and I think I have an understanding of signals and slots. I am writing a GUI that has various buttons that change the state of my main program. So for example in a drawing app, you would pick different drawing tools (using various buttons).

What is the best way to go about this? My first thought was to try to connect the clicked signal of the PushButton to some function that sets a current_tool variable. I did some searching and couldn't find a way to connect a QObject signal to a regular function.

This leads me to believe that there is probably a different approach. One where I create a new QObject (my own extension that is) that has various GUI properties. I would then define my slots here for the various buttons.

What is the best way to do this in QT. I am new and do not know of the preferred practice.

Any info would be useful, thanks

+1  A: 

You can define these "normal functions" as slots. Slots are just normal functions that can also be called by signals:

class ToolSelector : public QObject {
  Q_OBJECT

public:
  Tool *selected;

public slots:
  void selectBrush();
  void selectPen();
  void selectFill();
};

ToolSelector::selectBrush() {
  delete selected;
  selected = new Brush();
}

ToolSelector::selectPen() {
  // ...
}

// ...

toolsel = new ToolSelector();
brushButton = new QPushButton();
connect(brushButton, SIGNAL(clicked()), toolsel, SLOT(selectBrush()));
sth
It is important to note that this only works for objects derived by QObject.
ypnos
A: 

Inherit from the class that uic generates, creating, say, a MyAppWindow class. Provide extra METHODs in that class, as well as a Document or Drawing object. Connect these methods to the signals you're interested in, and them alter a member variable that contains the drawing state.

Lee B