Results 1 to 3 of 3

Thread: PopUp Widget coordinates are not set in respect of MainWindow

  1. #1
    Join Date
    Dec 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question PopUp Widget coordinates are not set in respect of MainWindow

    Hi Guys,

    What I'm trying to implement is custom PopUp Menu for custom PushButton placed in TollBar.


    PopUp menu is implemented via QWidget. For now it is simple black widget, in the end it should contain a number of PushButtons.

    Qt Code:
    1. #ifndef POPUPMENU_H
    2. #define POPUPMENU_H
    3.  
    4. #include <QWidget>
    5. #include <QPalette>
    6.  
    7.  
    8. namespace Ui {
    9. class PopUpMenu;
    10. }
    11.  
    12. class PopUpMenu : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit PopUpMenu(QWidget *parent);
    18. ~PopUpMenu();
    19. private:
    20. Ui::PopUpMenu *ui;
    21. std::unique_ptr<QPalette> pal;
    22. };
    23.  
    24. #endif // POPUPMENU_H
    25.  
    26. PopUpMenu::PopUpMenu(QWidget *parent) :
    27. QWidget(parent),
    28. ui(new Ui::PopUpMenu)
    29. {
    30. ui->setupUi(this);
    31.  
    32. this->setGeometry(parent->x(), parent->y() + parent.height(), parent->width() * 3, parent->height());
    33. //this->setGeometry(parent->rect().bottomLeft().x(), parent->rect().bottomLeft().y(), parent->width() * 3, parent->height());
    34. pal = std::make_unique<QPalette>();
    35. pal->setColor(QPalette::Window, QColor((Qt::black)));//testing
    36. this->setPalette(*pal);
    37. this->setWindowFlags(Qt::Popup);
    38. }
    39.  
    40. PopUpMenu::~PopUpMenu()
    41. {
    42. delete ui;
    43. }
    To copy to clipboard, switch view to plain text mode 


    In my button I simply create an instance of PopUpMenu and via reimplemented QWidget::enterEvent() show it buy invoking menu.show().

    the line this->setGeometry(parent->x(), parent->y() + parent.height(), parent->width() * 3, parent->height());


    works fine for size, but places the PopUp widget in respect of global display coordinates, that is, in top left corner of display, but not an application window.
    Tried mapFrom(To) functions - no luck.

    Need advise how to set the coordinates related to QWidget not to display.

    Thank you

  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: PopUp Widget coordinates are not set in respect of MainWindow

    You need to use QWidget::mapToGlobal() to translate your widget's pos() into screen coordinates, and use those coordinates in the setGeometry() call.
    <=== 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
    Dec 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PopUp Widget coordinates are not set in respect of MainWindow

    d_stranz
    Thank you VERY MUCH.

Similar Threads

  1. Replies: 0
    Last Post: 28th March 2013, 01:22
  2. Replies: 0
    Last Post: 6th November 2011, 10:22
  3. How to make a popup widget that looks like a popup?
    By Olivier Berten in forum Qt Programming
    Replies: 9
    Last Post: 6th June 2010, 14:19
  4. default popup of menubar / mainwindow
    By navid in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 18:29
  5. Absolute widget coordinates
    By rbp in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 05:30

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.