Results 1 to 8 of 8

Thread: QScrollBar arrow keys

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2018
    Posts
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default QScrollBar arrow keys

    I am displaying a scrolling page of line-oriented data. I want to control the display by specific lines and always know the index in my underlying data array of each line on the page, so I have my own scroll bar value-changed handling. When the value changes, I figure out what lines should be on the page and I update the display. For slider moves, it works fine. I want to arrange that the up/down arrows that come with the QScrollBar will move my index up one line (unless already at the top) or down one (unless already at the bottom). What I cannot find in the documentation is any way to capture a mouse click on the arrows. nextLine() and prevLine() don't do it. The value-changed signal does not let me distinguish arrow clicks. I need to know specifically when the user clicks on one of the arrows, just as with any control button I might create.

    Can someone tell me how to do this?

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QScrollBar arrow keys

    You want to change the single step and page step values. QAbstractSlider::setSingleStep() and QAbstractSlider::setPageStep().

    It isn't necessary to capture mouse clicks. When the user clicks on one of the arrows (or presses the up arrow or down arrow key) or clicks above or below the slider (or presses up / down arrow with Shift, or Page Up / Down) , the valueChanged() signal will be emitted with the new value incremented or decremented by the single or page step size respectively.

    You should consider simply using QListWidget or QListView to display your line-oriented data. It will probably do everything you want right out of the box without you doing anything more than supplying your data as a QStringList.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2018
    Posts
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QScrollBar arrow keys

    That doesn't work. I can't distinguish between a value change from an arrow key and an initial change caused by dragging the thumb. I don't want to totally reimplement just to switch to a QListView. As i indicated, I really want to keep complete control over the display. The idea of the system adjusting the display when scrolled based on a fixed page step will not work. I would really appreciate it if you or someone would answer my original question, or state that detecting a click on an arrow directly cannot be done.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QScrollBar arrow keys

    I would really appreciate it if you or someone would answer my original question
    I tried to. What I don't understand is why you think you need to distinguish between changing the slider position by dragging the thumb versus changing the position by clicking on the arrows or in the space above or below the thumb, by using the thumbwheel, or by an arrow up / down, page up / down, home / end key press.

    If you have done your math correctly and know how many lines fit on the display (which must be calculated to include font size and inter-line spacing), then you set the page step size equal to that value. You also set the scroll bar range knowing the full document range minus page size. If you haven't read the comments concerning these calculations in the QScrollBar documentation you should do so.

    If you really want to get into it, you can implement a slot for QAbstractSlider::actionTriggered() and manually change the slider position.

    The idea of the system adjusting the display when scrolled based on a fixed page step will not work.
    The "fixed page step" is not a constant; it depends on the number of lines in your document and the number of lines that can be displayed on one "page". With a new document or a resize of the page, it must be recalculated.

    This is why I suggested using QListView; it does all of these calculations for you based on the font and number of lines you load into the view and displays the correct part of the list in all cases. But if you insist that that "will not work", then you absolutely will have to reinvent the wheel and do all the calculations correctly including all the corner cases instead of using what the Qt developers have already done for you.

    By the way, you do not have to convert your current implementation to anything. You simply wrap your existing document with a class derived from QAbstractItemModel. Each line in your document is a row in the model, and it has only one column. It is flat, so items have no parents, and the only data to be returned is the text for each line. You need to implement the rowCount(), columnCount(), index(), parent(), and data() methods for the model. You can do each of those in as few as one line of code. From the document side, all it needs is methods that return the number of lines and the text for each line by index. You almost certainly have those methods already if you are laying out the display manually.

    You then pass a pointer to that wrapper model to the list view and your work is done. No calculations, no manual layout, nada, and you can go on to write the more important parts of your code.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Move rectangle with arrow keys
    By neda in forum Qt Quick
    Replies: 4
    Last Post: 8th May 2016, 12:06
  2. i need to use Alt+Ctrl+arrow keys combination
    By prasad.ece2 in forum Qt Programming
    Replies: 13
    Last Post: 27th September 2013, 07:34
  3. Detect Arrow Keys
    By Ebonair in forum Qt Programming
    Replies: 4
    Last Post: 1st October 2010, 03:55
  4. QScrollBar up-down arrow icon
    By zgulser in forum Qt Programming
    Replies: 5
    Last Post: 1st April 2010, 12:13
  5. How can I associate arrow keys?
    By Mariane in forum Newbie
    Replies: 2
    Last Post: 20th January 2006, 18:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.