简体   繁体   English

qt GUI连接

[英]qt GUI connecting

I am just starting out with QT. 我只是从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. 我正在编写一个具有各种按钮的GUI,这些按钮可以更改主程序的状态。 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. 我的第一个想法是尝试将PushButton的单击信号连接到设置current_tool变量的某个函数。 I did some searching and couldn't find a way to connect a QObject signal to a regular function. 我进行了一些搜索,但找不到将QObject信号连接到常规函数的方法。

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. 我在其中创建一个具有各种GUI属性的新QObject(即我自己的扩展名)。 I would then define my slots here for the various buttons. 然后,我将在此处为各种按钮定义插槽。

What is the best way to do this in QT. 在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. 从uic生成的类继承,创建一个MyAppWindow类。 Provide extra METHODs in that class, as well as a Document or Drawing object. 在该类中提供其他方法,以及Document或Drawing对象。 Connect these methods to the signals you're interested in, and them alter a member variable that contains the drawing state. 将这些方法连接到您感兴趣的信号,它们会更改包含绘图状态的成员变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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