系列文章目錄
前言
以前用的Qt5.15.2之前的版本,QtCreator默認的工程文件是*.pro,現在用5.15.2創(chuàng)建工程默認的工程文件是CMameList.txt,當然在創(chuàng)建項目時,仍然可以使用pro工程文件用QtCreator打開CMakeList.txt
以前用習慣了pro文件,現在改成CMakeList很不習慣,現在我們在CMakeList.txt中加入資源文件
一、加入圖片資源
1.首先,在Qt項目里創(chuàng)建一個目錄image,然后將圖片資源放image目錄中
2.在Qt creator中創(chuàng)建resource file
鼠標右鍵項目listWidgetSplitter> Add New… > Qt > Qt Resource File > 輸入文件名Resources,->next
3.新建資源文件.qrc
4.創(chuàng)建資源文件名Resources.qrc
5.把資源文件加入到你的工程中
6.并在CMakeLists.txt加入Resources.qrc并保存(control + s),這時左側項目工程會自動生成Resources.qrs
7.左側右鍵點擊Resources.qrs文件添加前綴
8.添加圖片,關聯(lián)到此前綴來:
右鍵·Resources.qrc > Open in Editor > 選中>Add Files > 從打開的文件選擇器中選擇icon1.png,icon2.png,padfsplit.ico,se_center.png
9.添加圖片,復制圖片路徑
在代碼中加入圖片路徑
二、代碼
頭文件文章來源:http://www.zghlxwxcb.cn/news/detail-814442.html
#ifndef LISTCONTROL_H
#define LISTCONTROL_H
#include <QWidget>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMenu>
#include <QSplitter>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QImage>
#include <QIcon>
#include <QMessageBox>
#include <QPushButton>
#include <QAction>
#include <QMouseEvent>
class listControl : public QWidget
{
Q_OBJECT
public:
explicit listControl(QWidget *parent = nullptr);
QListWidget* _listWgtLeft;
QListWidget* _listWgtRight;
QSplitter* _splitterMain;
QMenu* _popMenuLeft;
QMenu* _menuRight;
private:
void initUI();
signals:
private slots:
void onMenuPopSlot(const QPoint &pos);
};
#endif // LISTCONTROL_H
實現文件文章來源地址http://www.zghlxwxcb.cn/news/detail-814442.html
#include "listControl.h"
listControl::listControl(QWidget *parent)
: QWidget{parent}
{
initUI();
}
void listControl::initUI()
{
_splitterMain = new QSplitter(Qt::Horizontal, 0); //新建主分割窗口,水平分割
_listWgtLeft = new QListWidget(_splitterMain);
//設置樣式,直接在函數中設置
_listWgtLeft->setStyleSheet("QListWidget{border:1px solid gray; color:black; }"
"QListWidget::Item{padding-top:1px; padding-bottom:4px; }"
"QListWidget::Item:hover{background:skyblue; }"
"QListWidget::item:selected{background:lightgray; color:red; }"
"QListWidget::item:selected:!active{border-width:0px; background:lightgreen; }"
);
// _listWgtLeft->setResizeMode(QListView::Adjust); //適應布局調整
_listWgtLeft->setViewMode(QListView::ListMode);
_listWgtLeft->setMovement(QListView::Free);
_listWgtLeft->setContextMenuPolicy(Qt::CustomContextMenu);
_listWgtRight = new QListWidget(_splitterMain);
// QWidget* itemWgt = new QWidget(_listWgtLeft);
QGridLayout* itemMainLyt = new QGridLayout;
QHBoxLayout* itemContentLyt = new QHBoxLayout;
QListWidgetItem *item1 = new QListWidgetItem(_listWgtLeft);
item1->setFlags(item1->flags() | Qt::ItemIsEditable); // 設置item可編輯
QWidget *widget = new QWidget(_listWgtLeft);
QHBoxLayout *layout = new QHBoxLayout(widget);
QLabel* lbl01 = new QLabel("");
QImage* image01 = new QImage;
image01->load(":/images/icon/pdfsplit.ico");
lbl01->setPixmap(QPixmap::fromImage(*image01));
lbl01->setScaledContents(true);
QPixmap pixMapOgi01(":/images/icon/icon1.png");
QLabel *lbl02 = new QLabel(u8"衛(wèi)星軌道1測試");
QPushButton* btn01 = new QPushButton;
int btnWidth = btn01->width();
int btnHeight = btn01->height();
QPixmap pixmapFit = pixMapOgi01.scaled(btnWidth, btnHeight, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
btn01->setIcon(pixmapFit);
btn01->setStyleSheet(QString("QPushButton {background-color: transparent; }"));
btn01->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->addWidget(lbl01, 0, Qt::AlignVCenter | Qt::AlignLeft);
layout->addWidget(lbl02);
layout->addWidget(btn01, 0, Qt::AlignVCenter | Qt::AlignRight);
widget->setLayout(layout);
item1->setSizeHint(widget->sizeHint()); // 設置item大小
// item5->setData(Qt::UserRole, 1);
_listWgtLeft->setItemWidget(item1, widget); // 設置item控件
_splitterMain->setStretchFactor(0, 4);
_splitterMain->setStretchFactor(1, 6);
_splitterMain->setWindowTitle(tr("test splitter"));
itemMainLyt->addWidget(_splitterMain);
setLayout(itemMainLyt);
//右鍵彈出菜單
_popMenuLeft = new QMenu(_listWgtLeft);
QAction* addAct = new QAction(tr("add"));
QAction* resetHidAct = new QAction(tr("reset hide"));
QAction* cutAct = new QAction(tr("cut"));
QAction* copyAct = new QAction(tr("copy"));
QAction* delAct = new QAction(tr("delete"));
_popMenuLeft->addAction(addAct);
_popMenuLeft->addAction(resetHidAct);
_popMenuLeft->addAction(cutAct);
_popMenuLeft->addAction(copyAct);
_popMenuLeft->addAction(delAct);
connect(_listWgtLeft, &QListView::customContextMenuRequested, this, &listControl::onMenuPopSlot);
}
void listControl::onMenuPopSlot(const QPoint &pos)
{
// _popMenuLeft->exec(QCursor::pos());
_popMenuLeft->exec(_listWgtLeft->mapToGlobal(pos));
}
代碼調用
```cpp
#include "MainWindow.h"
#include "listControl.h"
#include <QApplication>
#include <QTextCodec>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();
a.setFont(QFont("Microsoft Yahei", 9));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
qDebug() << "中文調試信息";
QFont font("ZYSong18030" , 10);
a.setFont(font);
listControl* contrl = new listControl;
contrl->show();
return a.exec();
}
運行效果
到了這里,關于Qt5.15.2中加入圖片資源的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!