//设置窗口图标
setWindowIcon(QIcon("F:\\vs2022EngineFile\\QtVsProject\\Resource\\tubiao.ico"));
使用资源系统,将图标链接到exe中,可以实现跨平台
在vs中是没有办法创建资源文件的我们可以手动创建一个空的.qrc文件,然后打开。
但是vs并没有单独打开qrc文件的程序,不过有带打开qrc文件的插件,下载这个插件,然后把他拷贝到你的按照Qt目录的msvc套件的bin目录中
然后将资源文件添加的图标那个路径复制,用setWindowIcon进行设置图标即可
#include
#include
class Widget :public QWidget
{
Q_OBJECT
public:
Widget(QWidget* parent = nullptr) :QWidget(parent)
{
//设置窗口标题
this->setWindowTitle("小瓜");
//设置窗口图标 绝对路径
//setWindowIcon(QIcon("F:\\vs2022EngineFile\\QtVsProject\\Resource\\图标.ico"));
//使用资源文件进行设置窗口图标
setWindowIcon(QIcon(":/Resource/tubiao.ico"));
}
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
#include
#include
#include
class Widget :public QWidget
{
Q_OBJECT
public:
Widget(QWidget* parent = nullptr) :QWidget(parent)
{
//设置窗口标题
this->setWindowTitle("小瓜");
//设置窗口图标 绝对路径
//setWindowIcon(QIcon("F:\\vs2022EngineFile\\QtVsProject\\Resource\\图标.ico"));
//使用资源文件进行设置窗口图标
//setWindowIcon(QIcon(":/Resource/tubiao.ico"));
//相对路径
setWindowIcon(QIcon("Resource\\tubiao.ico"));
//返回当前程序的绝对路径
qDebug() << QDir::currentPath();
}
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
//设置窗口标题
this->setWindowTitle("小瓜");
//窗口大小、位置
qDebug() << width() << height() << size();
//在窗口没有创建之前,获取的坐标总是0
qDebug() << geometry() << frameGeometry() << rect();
//设置窗口大小
resize(400, 400);
//设置窗口位置,以边框为变化点
move(0, 0);
//设置固定窗口大小
setFixedSize(400, 400);
//设置最大、最小大小
setMinimumSize(400, 400);
setMaximumSize(400, 400);
//设置几何,以客户区为变换点
setGeometry(50, 50, 100, 100);
#include
#include
#include
#include
class Widget :public QWidget
{
Q_OBJECT
public:
Widget(QWidget* parent = nullptr) :QWidget(parent)
,btn(new QPushButton("按钮",this))
{
//设置窗口标题
this->setWindowTitle("小瓜");
//使用资源文件进行设置窗口图标
setWindowIcon(QIcon(":/Resource/tubiao.ico"));
//窗口大小、位置
qDebug() << width() << height() << size();
//在窗口没有创建之前,获取的坐标总是0
qDebug() << geometry() << frameGeometry() << rect();
//连接信号
connect(btn, &QPushButton::clicked, this, [=]()
{
qDebug() << geometry() << frameGeometry() << rect();
}
);
//设置窗口大小
resize(400, 400);
设置窗口位置,以边框为变化点
//move(0, 0);
设置固定窗口大小
//setFixedSize(400, 400);
设置最大、最小大小
//setMinimumSize(400, 400);
//setMaximumSize(400, 400);
//设置几何,以客户区为变换点
setGeometry(50, 50, 100, 100);
}
protected:
QPushButton* btn{};
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
#include
#include
#include
#include
class Widget :public QWidget
{
Q_OBJECT
public:
Widget(QWidget* parent = nullptr) :QWidget(parent)
,btn(new QPushButton("按钮",this))
,m_colseScreen(new QPushButton("X",this))
,m_max(new QPushButton("□",this))
,m_min(new QPushButton("_",this))
,m_fullScreen(new QPushButton("■",this)
)
{
//设置窗口大小
resize(400, 400);
//设置窗口标题
this->setWindowTitle("小瓜");
//使用资源文件进行设置窗口图标
setWindowIcon(QIcon(":/Resource/tubiao.ico"));
//设置按钮固定大小
m_colseScreen->setFixedSize(32, 32);
m_max->setFixedSize(32, 32);
m_min->setFixedSize(32, 32);
m_fullScreen->setFixedSize(32, 32);
//设置按钮位置
m_colseScreen->move(width() - m_colseScreen->width(), 0);
m_max->move(width() - m_max->width() * 2, 0);
m_min->move(width() - m_min->width() * 3, 0);
m_fullScreen->move(width() - m_fullScreen->width() * 4, 0);
//连接信号
connect(m_colseScreen, &QPushButton::clicked, this, [=]()
{
//关闭窗口
close();
}
);
connect(m_max, &QPushButton::clicked, this, [=]()
{
//判断窗口是不是最大化
if (isMaximized())
{
//恢复窗口,正常化
showNormal();
}
else
{
//最大化窗口
showMaximized();
}
}
);
connect(m_min, &QPushButton::clicked, this, [=]()
{
//最小化窗口
showMinimized();
}
);
connect(m_fullScreen, &QPushButton::clicked, this, [=]()
{
//判断是否是全屏
if (isFullScreen())
{
//恢复正常
showNormal();
}
else
{
//全屏
showFullScreen();
}
}
);
}
protected:
QPushButton* btn{};
QPushButton* m_max{};
QPushButton* m_min{};
QPushButton* m_fullScreen{};
QPushButton* m_colseScreen{};
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
newWidget->setAttribute(Qt::WA_DeleteOnClose);
#include
#include
#include
#include
#include
class Widget :public QWidget
{
Q_OBJECT
public:
Widget(QWidget* parent = nullptr) :QWidget(parent)
, btn(new QPushButton("按钮", this))
, m_close(new QPushButton("destroyed", this))
, newWidget(new QWidget)
{
//设置窗口大小
resize(400, 400);
//设置窗口标题
this->setWindowTitle("小瓜");
//使用资源文件进行设置窗口图标
setWindowIcon(QIcon(":/Resource/tubiao.ico"));
connect(btn, &QPushButton::clicked, this, [=]()
{
//判断这个窗口是否隐藏
if (newWidget->isHidden())
{
//显示这个窗口
newWidget->show();
}
else
{
//隐藏这个窗口
newWidget->hide();
}
}
);
//设置按钮右对齐
m_close->move(width() - m_close->width(), 0);
connect(newWidget, &QPushButton::destroyed, [=]()
{
qDebug() << "destroyed";
}
);
//设置属性,让窗口调用close时,自动销毁
newWidget->setAttribute(Qt::WA_DeleteOnClose);
connect(m_close, &QPushButton::clicked, this, [=]()
{
if (newWidget)
{
//如果程序存在多个窗口,那么关闭某一个窗口时,不是关闭是隐藏
newWidget->close();//隐藏窗口
//释放这个指针也能销毁,就可以不用使用close与设置属性了
//newWidget->deleteLater();
}
else
{
qDebug() << "newWidget is null";
}
}
);
//显示这个窗口
newWidget->show();
}
protected:
QPushButton* btn{};
QPushButton* m_close{};
//为了更安全,我们一般在这用智能指针释放更安全
QPointer<QWidget> newWidget{};
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
# 需求最低版本号
cmake_minimum_required(VERSION 3.24)
# 设置项目名
project(Project)
# 添加子目录
add_subdirectory(ProjectTwo)
add_subdirectory(ProjectOne)
#include
#include
#include
#include
class Widget :public QWidget
{
Q_OBJECT
public:
Widget(QWidget* parent = nullptr) :QWidget(parent)
, btn(new QPushButton("模态对话框(应用程序级别)", this))
, btn2(new QPushButton("显示半模态对话框(窗口级别)", this))
, btn3(new QPushButton("显示非模态对话框", this))
{
resize(800, 300);
btn->setFixedSize(200, btn->height());
btn2->setFixedSize(200, btn2->height());
btn3->setFixedSize(200, btn3->height());
btn2->move(btn->width(), 0);
btn3->move(btn->width() * 2, 0);
//显示模态对话框
connect(btn, &QPushButton::clicked,this,[]()
{
//显示对话框
QDialog loginDlg2;
//阻塞程序执行
loginDlg2.exec();
}
);
//显示半模态对话框
connect(btn2, &QPushButton::clicked, this, []()
{
//显示对话框
QDialog loginDlg;//这个必须设置为成员变量才行,否则直接销毁了,但是不经常使用
//不会阻塞程序执行,但是会阻塞窗口的消息,在消息层面会模态对话框一样(不让操作)
loginDlg.open();
}
);
//显示非模态对话框
connect(btn3, &QPushButton::clicked, this, [this]()
{
//不阻塞,使用这个就必须QDialog是成员变量,否则看不见效果
loginDlg.show();
}
);
}
protected:
QPushButton* btn{};
QPushButton* btn2{};
QPushButton* btn3{};
QDialog loginDlg;
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
#include
#include
#include
#include
class Dialog :public QDialog
{
Q_OBJECT
public:
Dialog(QWidget* parent = nullptr) :QDialog(parent)
, btn(new QPushButton("确认", this))
, btn2(new QPushButton("取消", this))
{
btn2->move(160, 0);
btn2->setDefault(true);//把取消设置为默认按钮,当按下回车,自动执行取消键
connect(btn, &QPushButton::clicked, this, [=]()
{
//accept();
//done可以任意发送一个整型数据
done(QDialog::Accepted);
}
);
connect(btn2, &QPushButton::clicked, this, [=]()
{
//rejected();
//close();
//比如我想拿到数据,因为窗口是阻塞的。为什么能获取数据,因为窗口结束了后,直接就结束了,都等不到信号与槽
done(QDialog::Rejected);
data = "小瓜";
}
);
}
//提供接口呗
QString getData() const
{
return data;
}
protected:
QPushButton* btn{};
QPushButton* btn2{};
QString data;
};
class Widget :public QWidget
{
Q_OBJECT
public:
Widget(QWidget* parent = nullptr) :QWidget(parent)
, btn(new QPushButton("模态对话框(应用程序级别)", this))
, btn2(new QPushButton("显示半模态对话框(窗口级别)", this))
, btn3(new QPushButton("显示非模态对话框", this))
{
resize(800, 300);
btn->setFixedSize(200, btn->height());
btn2->setFixedSize(200, btn2->height());
btn3->setFixedSize(200, btn3->height());
btn2->move(btn->width(), 0);
btn3->move(btn->width() * 2, 0);
//显示模态对话框
connect(btn, &QPushButton::clicked,this,[]()
{
//显示对话框
Dialog loginDlg2;
//阻塞程序执行
int value = loginDlg2.exec();
if (value == QDialog::Accepted)
{
qDebug() << "Accepted";
}
else
{
qDebug() << "Rejecet" << loginDlg2.getData();
}
}
);
//显示半模态对话框
connect(btn2, &QPushButton::clicked, this, []()
{
//显示对话框
QDialog loginDlg;//这个必须设置为成员变量才行,否则直接销毁了,但是不经常使用
//不会阻塞程序执行,但是会阻塞窗口的消息,在消息层面会模态对话框一样(不让操作)
loginDlg.open();
}
);
//显示非模态对话框
connect(btn3, &QPushButton::clicked, this, [this]()
{
//不阻塞,使用这个就必须QDialog是成员变量,否则看不见效果
loginDlg.show();
}
);
}
protected:
QPushButton* btn{};
QPushButton* btn2{};
QPushButton* btn3{};
QDialog loginDlg;
};
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
icon.rc
的文件,用文本编辑器打开这个icon.rc
文件写入如下内容。zay
是你自己的图标名字IDI_ICON1 ICON DISCARDABLE "zay.ico"
windeployqt AppName
,AppName
表示应用程序完整路径Name: "ChineseSimple";MessagesFile:"compiler:Languages\ChineseSimple.isl"