qt combox选中消息_Qt入门系列(二)

本文继续:

YYLi-CV:Qt入门系列(一)​zhuanlan.zhihu.com
zhihu-card-default.svg

作业1

需求:点击按钮打开,弹出一个新的菜单,单击按钮关闭,关闭新开的菜单

步骤:

1.新建一个QMainWindow项目

2.在1的基础上新建一个类QMainWIndow2

在Q.h中添加Q2对象和定义函数

//实例化对象
  private:
    QPushButton *btn1=new QPushButton("打开",this);
    QPushButton *btn2=new QPushButton("关闭",this);
    MainWindow2 w2;
    //实例化方法
  private slots:
    void showMainWindow2();
    void closeMainWIndow2();

在Q.cpp中实现

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    
​
    setFixedSize(600,600);
    //设置第一个pushbutton的属性
    btn1->move(100,100);
    btn1->resize(100,50);
​
    connect(btn1,&QPushButton::clicked,this,&MainWindow::showMainWindow2);
  
​
    //第二个pushbutton的属性
    btn2->move(100,200);
    btn2->resize(100,50);
    connect(btn2,&QPushButton::clicked,this,&MainWindow::closeMainWIndow2);
}
​
​
 void MainWindow::showMainWindow2()
{
    
     w2.show();
 }
​
 void MainWindow::closeMainWIndow2()
 {
    
     w2.close();
 }

结果显示:

a6d64c0caed7f47f43fe8f62d98dc281.png

4. QMainWindow

4.1 菜单栏和工具栏

Ubuntu下不显示菜单栏解决办法:

https://blog.csdn.net/z952957407/article/details/100850316

解决Ubuntu下Qt不能输入中文的方法:

https://blog.csdn.net/future_ai/article/details/80059338?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

中文字体下载链接:

https://github.com/fcitx/fcitx-qt5

Ubuntu在root下操作:

http://www.360doc.com/content/16/0408/16/25664332_548895388.shtml

基本操作:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    
​
     //窗口大小
    resize(600,400);
​
    //菜单栏只能有一个
    //创建菜单栏
    QMenuBar *bar=menuBar();
    //菜单栏放在系统中
    setMenuBar(bar);
​
    //创建菜单
    QMenu *fileMenu=bar->addMenu("文件");
    QMenu *editMenu=bar->addMenu("编辑");
​
    //创建菜单项
    QAction *newAction=fileMenu->addAction("新建");
    //添加分割线
    fileMenu->addSeparator();
    QAction *openAction= fileMenu->addAction("打开");
​
    //工具栏可以有多项
    QToolBar *toolBar=new QToolBar(this);
    addToolBar(Qt::LeftToolBarArea,toolBar);
​
    //只允许左右停靠
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值