zoukankan      html  css  js  c++  java
  • QT学习一:菜单

    // 创建菜单1

    1 QMenu *fileMenu = new QMenu(tr("&File"),this);  
    2 QAction *newAction = new QAction(tr("&New"),this);  
    3 fileMenu->addAction(newAction);  
    4 QMenuBar *menuBar = new QMenuBar(this);  
    5 menuBar->addMenu(fileMenu);  
    6 connect(newAction,SIGNAL(triggered()),this,SLOT(fileNew()));  

    为什么创建出来的菜单那么丑?菜单栏 file 右边有一块空白区域,上面没有任何显示。
    刚开始学习QT,在 connect 时如果没有将 fileNew() 在 .h 中声明为 slot,则上面的 connect 不会起到作用。
    这个需要注意,偶就犯过此类错误:在 .h 中是声明了,但不是声明在 slot 区。

    // 创建菜单2

     1 QMenu *fileMenu = new QMenu(tr("&File"),this);  
     2 QMenu *editMenu = new QMenu(tr("&Edit"),this);  
     3 QAction *newAction = new QAction(tr("&New"),this);  
     4 QAction *copyAction = new QAction(tr("&Copy"),this);  
     5 fileMenu->addAction(newAction);  
     6 editMenu->addAction(copyAction);  
     7 QMenuBar *menuBar = new QMenuBar(this);  
     8 menuBar->addMenu(fileMenu);  
     9 menuBar->addMenu(editMenu);  
    10 connect(newAction,SIGNAL(triggered()),this,SLOT(fileNew()));  
    11 connect(copyAction,SIGNAL(triggered()),this,SLOT(editCopy()));  

    增加一项 Edit 菜单后,创建的菜单栏与1的宽度是相同的,Edit 菜单项正好占用了1中的空白区域。

    // 创建菜单3

     1 QMenu *fileMenu = new QMenu(tr("&File"),this);  
     2 QMenu *editMenu = new QMenu(tr("&Edit"),this);  
     3 QMenu *helpMenu = new QMenu(tr("&Help"),this);  
     4 QAction *newAction = new QAction(tr("&New"),this);  
     5 QAction *copyAction = new QAction(tr("&Copy"),this);  
     6 QAction *aboutAction = new QAction(tr("&About"),this);  
     7 fileMenu->addAction(newAction);  
     8 editMenu->addAction(copyAction);  
     9 helpMenu->addAction(aboutAction);  
    10 QMenuBar *menuBar = new QMenuBar(this);  
    11 menuBar->addMenu(fileMenu);  
    12 menuBar->addMenu(editMenu);  
    13 menuBar->addMenu(helpMenu);  
    14 connect(newAction,SIGNAL(triggered()),this,SLOT(fileNew()));  
    15 connect(copyAction,SIGNAL(triggered()),this,SLOT(editCopy()));  
    16 connect(aboutAction,SIGNAL(triggered()),this,SLOT(helpAbout()));  

    再增加一个 Help 菜单时,发现显示与1相似,菜单项有一空白、只是多了一个指示,类似于 >>; 点击后才显示出 Edit 和 Help。
    怎么才能设定菜单栏的宽度,以避免出现1和3的问题?

    再在最后增加一句:ui->menuBar->setCornerWidget(menuBar,Qt::TopLeftCorner);
    就可以解决这个问题。

  • 相关阅读:
    HDU 1698 Just a Hook (线段树模板题-区间求和)
    spring定时任务详解(@Scheduled注解)
    spring定时任务(@Scheduled注解)
    java反射实现接口重试
    微信开发者工具在线调试
    消息队列应用场景
    Redis、Memcache和MongoDB的区别
    下拉框多选实现回显及sql
    MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法
    catch异常
  • 原文地址:https://www.cnblogs.com/91program/p/5194974.html
Copyright © 2011-2022 走看看