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);
    就可以解决这个问题。

  • 相关阅读:
    centos7安装apache http server启动失败--Failed to start The Apache HTTP Server.
    Centos之文件搜索命令find
    OpenSSL Heartbleed “心脏滴血”漏洞简单攻击示例
    nginx解析漏洞简介
    图片写入一句话木马
    cobaltstrike派生一个shell给metasploit
    Linux下date命令、格式化输出、时间设置
    msfvenom 常用生成 payload 命令
    metasploit派生一个shell给cobaltstrike
    centos 7 lsof简介
  • 原文地址:https://www.cnblogs.com/91program/p/5194974.html
Copyright © 2011-2022 走看看