Results 1 to 6 of 6

Thread: How to make my application screen-border stick

  1. #1
    Join Date
    Jun 2009
    Location
    Kraków, Poland
    Posts
    23
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default How to make my application screen-border stick

    I want my application to stick to the borders of screen when I move it - I mean when it's not further than 5px from the edge it should stick to it. (i. e. Winamp has it). How to achieve this? I can't do it in moveEvent, because of possible infinite recursion (it doesn't work, anyway :P) , overloading 'move' method doesn't work either.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make my application screen-border stick

    You can do it in moveEvent() Recursion won't be infinite if you provide a stop condition.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Location
    Kraków, Poland
    Posts
    23
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How to make my application screen-border stick

    OK, but it works not exactly as I want - when moving my app it doesn't stick (I mean "real-time" stick), and it changes it's position when Im dropping it - I would like my app to "real time" stick, if you know what I mean. Do I have to reimplement paintEvent(), or what?
    Thanks for quick answer.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make my application screen-border stick

    OK, but it works not exactly as I want - when moving my app it doesn't stick (I mean "real-time" stick), and it changes it's position when Im dropping it
    If you keep your app real time sticky to the corner, how would the user feel he has moved the window a certain distance ?
    I guess the above working is fair enough.

  5. #5
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Post Re: How to make my application screen-border stick

    If your window is a frameless windows(via Qt::FramelessWindowHint) you can easily manage that in "real-time".
    Following the "Analog-Clock" example, you can move the window by sub-classing both mousePressEvent() and mouseMoveEvent().
    Here is a sample app that does just that:

    Widget.cpp:
    Qt Code:
    1. Widget::Widget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget)
    4. {
    5. ui->setupUi(this);
    6. setWindowFlags(Qt::FramelessWindowHint);
    7. filter = new EventFilter(this);
    8. ui->widget->installEventFilter(filter);
    9. move(300,0);
    10. }
    11.  
    12. void Widget::mouseMoveEvent(QMouseEvent *event)
    13. {
    14. if(filter->canMove)
    15. {
    16. if (event->buttons() & Qt::LeftButton)
    17. {
    18. if(event->globalY() < 100){
    19. qDebug() << "click move" << event->globalY();
    20. move(event->globalPos() - dragPosition);
    21. }else{
    22. move(event->globalX() - dragPosition.x(),100);
    23. }
    24. event->accept();
    25. }
    26. }
    27.  
    28. }
    29. void Widget::mousePressEvent(QMouseEvent *event)
    30. {
    31. qDebug() << "click away" << filter->canMove;
    32. if (event->button() == Qt::LeftButton) {
    33. dragPosition = event->globalPos() - frameGeometry().topLeft();
    34. event->accept();
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    Widget.h:
    Qt Code:
    1. #include "eventFilter.h"
    2.  
    3. namespace Ui {
    4. class Widget;
    5. }
    6.  
    7. class Widget : public QWidget {
    8. Q_OBJECT
    9. public:
    10. Widget(QWidget *parent = 0);
    11. ~Widget();
    12.  
    13. protected:
    14. void changeEvent(QEvent *e);
    15. void mouseMoveEvent(QMouseEvent *event);
    16. void mousePressEvent(QMouseEvent *event);
    17.  
    18. private:
    19. Ui::Widget *ui;
    20. QPoint dragPosition;
    21. EventFilter *filter;
    22. };
    To copy to clipboard, switch view to plain text mode 

    eventFilter.h:
    Qt Code:
    1. class EventFilter : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. EventFilter(QWidget *parent){
    7. //
    8. canMove = false;
    9. }
    10. bool canMove;
    11.  
    12. private:
    13. QPoint dragPosition;
    14.  
    15. protected:
    16. bool eventFilter(QObject *obj, QEvent *event)
    17. {
    18. if (event->type() == QEvent::MouseButtonPress ){
    19. canMove = true;
    20. }
    21. else if(event->type() ==QEvent::MouseMove){
    22. canMove = true;
    23. }else{
    24. canMove = false;
    25. }
    26. return QObject::eventFilter(obj, event);
    27. }
    28. };
    To copy to clipboard, switch view to plain text mode 

    Keep in mind that if the user somehow moves your window(Windows can do that sometimes when it crashes or goes slow), you won't be able to move it back to the border until you restart your app. But if you check on moveEvent(), you can send it back if that ever happens...
    Hope you find it useful.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to make my application screen-border stick

    Quote Originally Posted by Fenix Voltres View Post
    OK, but it works not exactly as I want - when moving my app it doesn't stick (I mean "real-time" stick), and it changes it's position when Im dropping it - I would like my app to "real time" stick, if you know what I mean.
    Your system delivers move events only when the move is complete. It's either that or reconfigure/reimplement your system to deliver move events on the fly (it's often called "opaque moving" but whether it will work here depends on how it is implemented) or do your own moving (which will probably slow down your application). WinAmp probably does the latter.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 13th July 2009, 18:18
  2. Replies: 5
    Last Post: 14th April 2009, 08:27
  3. Replies: 1
    Last Post: 27th March 2008, 15:10
  4. Replies: 2
    Last Post: 12th November 2006, 08:47
  5. how to make the form full screen
    By shrikarcse in forum Qt Tools
    Replies: 1
    Last Post: 24th April 2006, 15:30

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.