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. How can I use QWidgetAction
Forum Updated to NodeBB v4.3 + New Features

How can I use QWidgetAction

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 633 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.
  • leekOnionAggeL Offline
    leekOnionAggeL Offline
    leekOnionAgge
    wrote on last edited by leekOnionAgge
    #1

    Hi all :
    I am a fresher of QT, and I want to implement the function like the below!(click the ToolButton/QAction, then a custom defined widget)
    9640ae0d-7e92-4763-952b-80496c16bb86-image.png image url

    and my code is as below:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        QToolBar        *toolBar;
        QToolButton     *toolButton01;
        QMenuBar        *menuBar01;
        QMenu           *menu01;
        QWidgetAction   *widgetAction;
        QWidget         *tempWidget;
        QAction         *tempAction;
    };
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        this->resize(500, 500);
        this->setWindowTitle("Hello World");
    
        toolBar = new QToolBar(this);
        toolButton01    = new QToolButton(toolBar);
        toolButton01->setText("HJY");
    
        toolButton01->setAutoRaise(true);
        toolButton01->setToolButtonStyle(Qt::ToolButtonTextOnly);
        toolButton01->setPopupMode(QToolButton::MenuButtonPopup);
        toolButton01->setToolTip(tr("Nice"));
    
        menu01          = new QMenu(toolButton01);
    #if 1
        widgetAction    = new QWidgetAction(menu01);
        tempWidget      = new QWidget();
        tempWidget->resize(100, 100);
        widgetAction->setDefaultWidget(tempWidget);
        menu01->addAction(widgetAction);
    #else
        tempAction      = new QAction("HHH", toolButton01);
        menu01->addAction(tempAction);
    #endif
        toolButton01->setMenu(menu01);
        toolBar->addWidget(toolButton01);
    
        this->addToolBar(toolBar);
    
    }
    
    MainWindow::~MainWindow()
    {
    }
    

    85c81ce8-8609-43b8-acbd-b042737d92ab-image.png 7c1e3c45-05e6-4c25-b47e-a1ecdf33471d-image.png

    But when I add QWidgetAction, It can`t show my defined widget!

    any kindly person can point my fault? thank you in advance!!!

    JonBJ 1 Reply Last reply
    0
    • leekOnionAggeL leekOnionAgge

      Hi all :
      I am a fresher of QT, and I want to implement the function like the below!(click the ToolButton/QAction, then a custom defined widget)
      9640ae0d-7e92-4763-952b-80496c16bb86-image.png image url

      and my code is as below:

      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
          QToolBar        *toolBar;
          QToolButton     *toolButton01;
          QMenuBar        *menuBar01;
          QMenu           *menu01;
          QWidgetAction   *widgetAction;
          QWidget         *tempWidget;
          QAction         *tempAction;
      };
      
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
      {
          this->resize(500, 500);
          this->setWindowTitle("Hello World");
      
          toolBar = new QToolBar(this);
          toolButton01    = new QToolButton(toolBar);
          toolButton01->setText("HJY");
      
          toolButton01->setAutoRaise(true);
          toolButton01->setToolButtonStyle(Qt::ToolButtonTextOnly);
          toolButton01->setPopupMode(QToolButton::MenuButtonPopup);
          toolButton01->setToolTip(tr("Nice"));
      
          menu01          = new QMenu(toolButton01);
      #if 1
          widgetAction    = new QWidgetAction(menu01);
          tempWidget      = new QWidget();
          tempWidget->resize(100, 100);
          widgetAction->setDefaultWidget(tempWidget);
          menu01->addAction(widgetAction);
      #else
          tempAction      = new QAction("HHH", toolButton01);
          menu01->addAction(tempAction);
      #endif
          toolButton01->setMenu(menu01);
          toolBar->addWidget(toolButton01);
      
          this->addToolBar(toolBar);
      
      }
      
      MainWindow::~MainWindow()
      {
      }
      

      85c81ce8-8609-43b8-acbd-b042737d92ab-image.png 7c1e3c45-05e6-4c25-b47e-a1ecdf33471d-image.png

      But when I add QWidgetAction, It can`t show my defined widget!

      any kindly person can point my fault? thank you in advance!!!

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @leekOnionAgge said in How can I use QWidgetAction:

      toolButton01->setToolTip(tr("f*** !!"));

      Please think about young children reading your post as their introduction to programming!

      Your code is correct. Your problem is that the added widget does not have anything on it taking space to show. If you replace it with tempWidget = new QLabel("Hello") it will be visible. Once you start putting stuff on your QWidget it will show, e.g.

          tempWidget      = new QWidget();
      //    tempWidget      = new QLabel("Jon");
          tempWidget->resize(100, 100);
          QGridLayout *grid = new QGridLayout;
          grid->addWidget(new QLabel("Hello"));
          tempWidget->setLayout(grid);
      

      you will see it. For whatever reason, the tempWidget->resize(...); is having no effect.

      leekOnionAggeL 1 Reply Last reply
      3
      • JonBJ JonB

        @leekOnionAgge said in How can I use QWidgetAction:

        toolButton01->setToolTip(tr("f*** !!"));

        Please think about young children reading your post as their introduction to programming!

        Your code is correct. Your problem is that the added widget does not have anything on it taking space to show. If you replace it with tempWidget = new QLabel("Hello") it will be visible. Once you start putting stuff on your QWidget it will show, e.g.

            tempWidget      = new QWidget();
        //    tempWidget      = new QLabel("Jon");
            tempWidget->resize(100, 100);
            QGridLayout *grid = new QGridLayout;
            grid->addWidget(new QLabel("Hello"));
            tempWidget->setLayout(grid);
        

        you will see it. For whatever reason, the tempWidget->resize(...); is having no effect.

        leekOnionAggeL Offline
        leekOnionAggeL Offline
        leekOnionAgge
        wrote on last edited by
        #3

        @JonB sorry for may rudely behavior! and I will keep more polite in the future!!
        thank you for you advice!!!

        1 Reply Last reply
        1

        • Login

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