Results 1 to 5 of 5

Thread: Changing the mouse button which selects items

  1. #1
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Changing the mouse button which selects items

    I'm trying to create a level editor with re-implemented QTableWidget and QTableWidgetItem.

    The thing is, I want to implement the mouse events such that the Middle Mouse Button corresponds to selection instead of the Left Mouse Button (so that the left mouse button only places the tiles). I want to do this with a ContiguousSelection Selection Mode.

    I really haven't been able to figure this out. I know in my re-implementations of mousePressEvent and mouseMoveEvent I can setSelection (even with this approach, I dunno how to set the QRect for a single item selection), but I want to know if there is a better way.

    Thanks for your help!

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Changing the mouse button which selects items

    If I understand You correctly why not to check in reimp. mouseEvent what mouse key was pressed and act accordingly?
    I.e.:
    Qt Code:
    1. void myTableView::mouseReleaseEvent(QMouseEvent *e)
    2. {
    3. if( e->button() == Qt::LeftButton )
    4. doSelection();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Or use setSelectionModel() and subclass QItemSelectionModel accordingly to above (in that case ignore mouse event in subclassed TableView and do selection in SelectionModel).
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Changing the mouse button which selects items

    Thanks for your reply!

    So, if I do something like this:

    Qt Code:
    1. void mousePressEvent(QMouseEvent *event)
    2. {
    3. if(event->button() == Qt::MidButton)
    4. {
    5. doSelection();
    6. setRectStartPoint( event->pos() );
    7. }
    8. }
    9.  
    10. void mouseReleaseEvent(QMouseEvent *event)
    11. {
    12. if(event->buttons() & (Qt::LeftButton|Qt::RightButton) )//This is to check that both Left & Right
    13. { //Were released. Or so I understand
    14. setTileOnTop( indexAt( event->pos() ) ); //from the documentation. Please tell
    15. } //if I'm wrong
    16. else if(event->button() == Qt::LeftButton)
    17. {
    18. setTile( indexAt( event->pos() ) );
    19. }
    20. else if(event->button() == Qt::RightButton)
    21. {
    22. removeTile( indexAt( event->pos() ) );
    23. }
    24. }
    25.  
    26. void mouseMoveEvent(QMouseEvent *event)
    27. {
    28. if(event->buttons() & Qt::MidButton)
    29. {
    30. doSelectionOfRect( event->pos() );
    31. }
    32. else if(event->buttons() &Qt::LeftButton)
    33. {
    34. setTile( indexAx( event->pos() ) );
    35. }
    36. else if(event->buttons() & Qt::RightButton)
    37. {
    38. removeTile( indexAt( event->pos() ) );
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

    Will it work? Will I only do the selection with MidButton and nothing else?

    Because if it will, then my problem is solved.

  4. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Changing the mouse button which selects items

    That should (probably because I didn't run Your code) do the trick.
    Basically as long as You don't pass event to default imp. i.e. QView::mouseReleaseEvent( event ); then selection on mouse pressEvent don't occure. In Your case (above code) You check if it is middle button so It should be ok.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  5. #5
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Another question, this time about QImages and QClipboard

    It worked, thanks for your help!

    I have another question about my Level Editor, but it does not relate to the mouse-selection issue. I dunno if I can ask it here or if I'll have to make an entirely new topic.

    The issue is about copying and pasting using the QClipboard. In my editor, each Tile (re-implemented QTableWidgetItem) has a QImage for the tile image and an unsigned long for the tile's map number.

    What I want is, when my Level (re-implemented QTableWidget) has its copy slot called, it copies both the image and the code to the clipboard, for each tile in the selection range. And when paste is called, it puts the copied Qimage and unsigned long for each tile, in the correct tile.

    Now, I know absolutely nothing about the specifics of clipboard and QMimeData. And I dunno if something like this would work for each tile;

    Qt Code:
    1. void copy()
    2. {
    3. ...
    4. //For each tile t in the selection range
    5. QMimeData *data = new QMimeData;
    6.  
    7. //The following is most probably wrong, since I don't think I can set both text
    8. //data and image data for the same QMimeData...
    9.  
    10. data->setImageData( t->image() );
    11. data->setText( QString::number( t->mapNumber(), 16 ).toUpper() ) );
    12. QApplication::clipboard()->setMimeData( data );
    13. ...
    14. }
    15.  
    16. void PasteCommand::redo()
    17. {
    18. ...
    19. //For each tile t in the selection range
    20. QMimeData *data = QApplication::clipboard()->mimeData();
    21.  
    22. //Again, the inclusion of two different types of data in the same QMimeData
    23. //is most probably wrong...
    24.  
    25. if( data->hasImage() )
    26. t->setImage( data->imageData() );
    27. if( data->hasText() )
    28. t->setMapNum( numberFromText( data->text() ) );
    29.  
    30. ...
    31. }
    To copy to clipboard, switch view to plain text mode 

    I really have no clue on how to go about doing this.

    I had an idea to copy only the map numbers to the clipboard, and then include the images corresponding to the number from my tile boxes (they hold the tiles imported from the tile sheets) when paste is called, but the code to get the image for each map number would be impractical. Furthermore, I allow the editor to place one type of tile on top of the other, creating a new tile with a new number. So, if I encounter such a tile, I'll first have to separate the number and get individual tiles and then set one on top of the other again.

    I can always not include copy() and paste() functionality since I'm the only one using the editor, but I guess I shouldn't do things by half standards. And besides, it'll be a good learning experience.

    Thanks for your help! And please tell me if I should make a new topic for this question.

Similar Threads

  1. Replies: 2
    Last Post: 28th October 2016, 11:12
  2. QTabWidget currentChanged signal for repeated selects of a tab
    By balasaravanan in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2011, 06:04
  3. Changing text of button in no relation to button
    By Sabre Runner in forum Newbie
    Replies: 22
    Last Post: 23rd September 2010, 12:29
  4. Replies: 3
    Last Post: 19th November 2009, 14:10
  5. changing mouse pointer
    By bhogasena in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2009, 15:57

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.