简体   繁体   中英

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

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()));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM