c++中如何调出新窗口并显示想要的图
时间: 2024-09-15 07:09:27 浏览: 57
在C++中,如果你想在程序中打开一个新的窗口并显示图片,你可以使用图形用户界面库,如Qt、wxWidgets或者Windows API等。这里以Qt为例说明:
首先,你需要安装Qt库,并且在你的项目中包含相关的头文件和链接相应的库。
```cpp
#include <QApplication>
#include <QImageReader>
#include <QLabel>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// 创建一个窗口
QWidget *window = new QWidget();
window->setWindowTitle("显示图片");
// 创建一个 QLabel 来显示图片
QLabel *label = new QLabel(window);
label->setAlignment(Qt::AlignCenter); // 设置图片居中
// 使用 QImageReader 读取图片
QImage image;
if (!image.load("path_to_your_image.jpg")) {
qDebug() << "无法加载图片";
return 1;
}
// 将图片设置给 QLabel
label->setPixmap(QPixmap::fromImage(image));
// 显示窗口
window->show();
// 获取屏幕大小
QRect screenRect = QApplication::desktop()->screenGeometry();
// 计算窗口的位置使其位于屏幕中心
int x = screenRect.width() / 2 - window->width() / 2;
int y = screenRect.height() / 2 - window->height() / 2;
// 设置窗口的位置
window->move(x, y);
return app.exec(); // 进入主事件循环
}
```
在这个例子中,你需要将`"path_to_your_image.jpg"`替换为你要显示的实际图片路径。
阅读全文
相关推荐


















