异度部落格

学习是一种生活态度。

0%

QT学习笔记之十一 SymbolPicker

最近太忙了,都没怎么看 Qt 鄙视下自己....

不说了,放代码

symbolpicker.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef SYMBOLPICKER_H
#define SYMBOLPICKER_H
#include <QtGui/QDialog>
#include <QMap>
class QDialogButtonBox;
class QIcon;
class QListWidget;
namespace Ui
{
class SymbolPicker;
}
class SymbolPicker : public QDialog
{
Q_OBJECT
public:
SymbolPicker(const QMap<int,QString> &symbolMap,QWidget *parent = 0);
~SymbolPicker();
int selected() const {return id;}
void done(int result);
private:
Ui::SymbolPicker *ui;
QIcon iconForSymbol(const QString &symbolName);
QListWidget *listWidget;
QDialogButtonBox *buttonBox;
int id;
};
#endif // SYMBOLPICKER_H

symbolpicker.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<QtGui>
#include "symbolpicker.h"
#include "ui_symbolpicker.h"
SymbolPicker::SymbolPicker(const QMap<int,QString> &symbolMap,QWidget *parent)
: QDialog(parent), ui(new Ui::SymbolPicker)
{
ui->setupUi(this);
id = -1; //设置id默认值
listWidget = new QListWidget;
listWidget->setIconSize(QSize(60,60));
QMapIterator<int,QString> i(symbolMap);
while(i.hasNext()) {
i.next();
QListWidgetItem *item = new QListWidgetItem(i.value(),listWidget);
item->setIcon(iconForSymbol(i.value()));
item->setData(Qt::UserRole,i.key());
}
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok| QDialogButtonBox::Cancel);
connect(buttonBox,SIGNAL(accepted()),this,SLOT(accept()));
connect(buttonBox,SIGNAL(rejected()),this,SLOT(reject()));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(listWidget);
layout->addWidget(buttonBox);
setLayout(layout);
setWindowTitle(tr("Symbol Picker"));
}
SymbolPicker::~SymbolPicker()
{
delete ui;
}
void SymbolPicker::done(int result)
{
id = -1;
if (result == QDialog::Accepted) {
QListWidgetItem *item = listWidget->currentItem();
if (item)
id = item->data(Qt::UserRole).toInt();
}
QDialog::done(result);
}
QIcon SymbolPicker::iconForSymbol(const QString &symbolName)
{
QString fileName = ":/images/" + symbolName.toLower();
fileName.replace(' ','-');
return QIcon(fileName);
}

symbolpicker.qrc (资源文件)

1
2
3
4
5
<RCC>
<qresource>
<file>images/killua.png</file>
</qresource>
</RCC>

刚开始 icon 显示一直有问题,后来配置了下资源文件就 OK 了...