本文继续:
YYLi-CV:Qt入门系列(一)zhuanlan.zhihu.com作业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](https://img-blog.csdnimg.cn/img_convert/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);
//只允许左右停靠