Qt World Summit: Submit your Presentation

Synchronize QTextEdit Sliders

  • Hello Qt Dudes,

    for text file comparison I have two QTextEdit fields which placed side by side. Now I want to synchonize the vertical slider for a better outline.

    The problem is, the slider are not really in sync. As you can see on QTextEdit is slightly shifted.
    Here is the implementation of the sync code:

    	template<typename TTextWidget>
    	static void impl_after_content(std::array<QWidget*, 2>& aWidgets)
    			TTextWidget* left = dynamic_cast<TTextWidget*>(aWidgets[0]);
    			TTextWidget* right = dynamic_cast<TTextWidget*>(aWidgets[1]);
    			if( nullptr != left && nullptr != right) {
    				auto slider_sync_fun = [](TTextWidget* aLeft, TTextWidget* aRight) {
    					auto left_value = aLeft->verticalScrollBar()->value();
    					int slider_val = left_value;
    					slider_val = slider_val > aRight->verticalScrollBar()->maximum() ?
    							aRight->verticalScrollBar()->maximum() : slider_val;
    				QObject::connect(left->verticalScrollBar(), &QScrollBar::actionTriggered, [slider_sync_fun, left,right](){
    					slider_sync_fun(left, right);
    				QObject::connect(right->verticalScrollBar(), &QScrollBar::actionTriggered, [slider_sync_fun, left,right](){
    					slider_sync_fun(right, left );

    Please note that all two sided have the same length because missing lines are filled with blank lines.

    Do you have a hint for me what go wrong here

    Thank you for reading

  • Moderators

    Hi, welcome to the forum.

    You said that both sides have the same height yet you check for exceeding maximum. If both sides had the same height the maximums should be equal so no need for the check. If the check is valid then it suggests the maximums can differ, so first thing to check is if the maximums are actually the same.
    Print out the values on the left and right and the maximums, not just the slider_val.

    If the maximums differ then there are couple of things you could check:
    -is the number of actual newlines the same on both sides?
    -is the font size exactly the same on both sides?
    -do you have word wrapping enabled and left and right wrap differently?

  • Doesn't it have something to do with the font size also? I mean, the metrics between a regular letter and a . (dot) is different, right?

Log in to reply