Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Synchronize QTextEdit Sliders

Synchronize QTextEdit Sliders

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 764 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    awaken1988
    wrote on last edited by
    #1

    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.
    0_1526742013676_sync_sliders.png

    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)
    	{
    		//QPlainTextEdit
    		{
    			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;
    					aRight->verticalScrollBar()->setValue(slider_val);
    
    					std::cout<<"slider_val="<<slider_val<<std::endl;
    				};
    
    				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 );
    				});
    
    				return;
    			}
    		}
    	}
    

    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

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mr Gisa
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved