简体   繁体   English

C++ QT 创建者为 QTextEdit::verticalSlideBar 创建插槽 function?

[英]C++ QT Creator create slot function for QTextEdit::verticalSlideBar?

I'm using QT Creator to create a simple Application that has two textEdit fields next to each oteher.我正在使用 QT Creator 创建一个简单的应用程序,每个应用程序旁边都有两个 textEdit 字段。 I want both fields to be linked when it comes to scrolling so that when one field scrolls up or down, the other one will as well automatically and vice versa.我希望在滚动时将两个字段链接起来,这样当一个字段向上或向下滚动时,另一个字段也会自动滚动,反之亦然。 For this, I need a callback function that is triggered whenever the user moves the slideBar of one of the fields.为此,我需要一个回调 function,只要用户移动其中一个字段的滑动条就会触发该回调。 Unfortunately, when I right click the textEdit fields and press "Go to slots" I can not find an event for the movement of the slideBar.不幸的是,当我右键单击 textEdit 字段并按“转到插槽”时,我找不到滑动条移动的事件。

How can I achieve this?我怎样才能做到这一点?

QTextEdit does not have a signal for when the sliderbar in it changes, since it is not a scrollbar. QTextEdit没有关于其中的滑块何时更改的信号,因为它不是滚动条。 However QScrollBar has the sliderMoved(int value) signal which is emitted when the slider moves.但是QScrollBar具有在 slider 移动时发出的sliderMoved(int value)信号。 QScrollBar also has a way to set its scroll value via slots (with setValue(int value) ) QScrollBar还可以通过槽设置其滚动值(使用setValue(int value)

We can therefore tie two scrollbars together using signals and slots very easily.因此,我们可以很容易地使用信号和插槽将两个滚动条绑定在一起。

For example:例如:

... 
// Get easy pointers to the scrollbars
QScrollBar* textbx_slider_1 = ui->textbx1->verticalScrollBar();
QScrollBar* textbx_slider_2 = ui->textbx2->verticalScrollBar();

// Connect them too each other
connect(textbx_slider_1, &QScrollBar::sliderMoved, textbx_slider_2, &QScrollBar::setValue); // Connect the first scrollbar to the second

connect(textbx_slider_2, &QScrollBar::sliderMoved, textbx_slider_1, &QScrollBar::setValue); // Connect the second scrollbar to the first
...

(This assumes that your QTextEdit widgets have ids' of textbx1 and textbx2 ) (这假设您的QTextEdit小部件的 ID 为textbx1textbx2


Edit:编辑:

It is worth mentioning that sliderMoved will not be emitted when using the scroll wheel on the text box.值得一提的是,在文本框上使用滚轮时不会发出sliderMoved To detect those inputs you must use something like QScrollBar::valueChanged .要检测这些输入,您必须使用QScrollBar::valueChanged类的东西。 You have to be careful with this however since setValue emits valueChanged , meaning you will get an infinite feedback loop if you simply modify the above code.但是,您必须小心这一点,因为setValue会发出valueChanged ,这意味着如果您简单地修改上述代码,您将获得无限的反馈循环。

To prevent this you could use a lambda, something like this:为了防止这种情况,您可以使用 lambda,如下所示:

...
int old_pos = textbx_slider_1->value()
std::function<void(int, QScrollBar*)> f = [old_pos](int new_pos, QScrollBar* slider){
    if (new_pos != old_pos) {
        // Only trigger if the value has changed
        slider->setValue(new_pos);
        old_pos = new_pos;
    };
connect(textbx_slider_1, &QScrollBar::sliderMoved, std::bind(f, std::placeholders::_1, textbx_slider_2)); // Connect the first scrollbar to the second

connect(textbx_slider_2, &QScrollBar::sliderMoved, std::bind(f, std::placeholders::_1, textbx_slider_1)); // Connect the second scrollbar to the first
...

(The weirdness with std::bind() is simply so we don't repeat virtually the same lambda twice) std::bind()的奇怪之处在于我们不会重复几乎相同的 lambda 两次)

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

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