Qt學習筆記,使用QTreeWidget實現如Eva的用戶列表
完整源碼:
#-------------------------------------------------
#
# Project created by QtCreator 2011-10-08T14:23:35
#
#-------------------------------------------------
QT += core gui
TARGET = drawItem
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
RESOURCES += \
icon.qrc
#-------------------------------------------------
#
# Project created by QtCreator 2011-10-08T14:23:35
#
#-------------------------------------------------
QT += core gui
TARGET = drawItem
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
FORMS += widget.ui
RESOURCES += \
icon.qrc
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>349</width>
<height>522</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/theme/contact.png</normaloff>:/new/prefix1/image/theme/contact.png</iconset>
</attribute>
<attribute name="title">
<string/>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QTreeWidget" name="treeWidget">
<property name="toolTip">
<string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;">
<p align="justify" style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"></p></body></html></string>
</property>
<property name="styleSheet">
<string notr="true">font: 11pt "Ubuntu";
text-decoration: underline;</string>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="iconSize">
<size>
<width>50</width>
<height>50</height>
</size>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<property name="headerHidden">
<bool>true</bool>
</property>
<column>
<property name="text">
<string>1</string>
</property>
</column>
<item>
<property name="text">
<string>Friends</string>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="textAlignment">
<set>AlignHCenter|AlignVCenter|AlignCenter</set>
</property>
<property name="background">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>9</red>
<green>9</green>
<blue>193</blue>
</color>
</brush>
</property>
<property name="foreground">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</property>
<item>
<property name="text">
<string>Friend3
<this is comment></string>
</property>
<property name="font">
<font>
<weight>75</weight>
<italic>false</italic>
<bold>true</bold>
<underline>true</underline> www.2cto.com
<stylestrategy>PreferAntialias</stylestrategy>
</font>
</property>
<property name="textAlignment">
<set>AlignLeft|AlignVCenter</set>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/face/101.png</normaloff>:/new/prefix1/image/face/101.png</iconset>
</property>
<property name="flags">
<set>ItemIsSelectable|ItemIsEditable|ItemIsDragEnabled|ItemIsUserCheckable|ItemIsEnabled</set>
</property>
<item>
<property name="text">
<string>Find</string>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/theme/find.png</normaloff>:/new/prefix1/image/theme/find.png</iconset>
</property>
</item>
<item>
<property name="text">
<string>Email</string>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/theme/chat.png</normaloff>:/new/prefix1/image/theme/chat.png</iconset>
</property>
</item>
<item>
<property name="text">
<string>Camera</string>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/theme/qq_album.png</normaloff>:/new/prefix1/image/theme/qq_album.png</iconset>
</property>
</item>
</item>
<item>
<property name="text">
<string>Friend2</string>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/face/111.png</normaloff>:/new/prefix1/image/face/111.png</iconset>
</property>
</item>
<item>
<property name="text">
<string>Friend1</string>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/face/126.png</normaloff>:/new/prefix1/image/face/126.png</iconset>
</property>
</item>
</item>
<item>
<property name="text">
<string>Devices</string>
</property>
<property name="textAlignment">
<set>AlignHCenter|AlignVCenter|AlignCenter</set>
</property>
<property name="background">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>0</red>
<green>85</green>
<blue>255</blue>
</color>
</brush>
</property>
<property name="foreground">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>170</red>
<green>31</green>
<blue>73</blue>
</color>
</brush>
</property>
<item>
<property name="text">
<string>Dev2</string>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/theme/delgroup.png</normaloff>:/new/prefix1/image/theme/delgroup.png</iconset>
</property>
</item>
<item>
<property name="text">
<string>Dev1</string>
</property>
<property name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/theme/delgroup.png</normaloff>:/new/prefix1/image/theme/delgroup.png</iconset>
</property>
</item>
</item>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="icon">
<iconset resource="icon.qrc">
<normaloff>:/new/prefix1/image/theme/quns.png</normaloff>:/new/prefix1/image/theme/quns.png</iconset>
</attribute>
<attribute name="title">
<string/>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="icon.qrc"/>
</resources>
<connections/>
</ui>
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QModelIndex>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
private slots:
void changeText(const QModelIndex &index);
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
#include <QtCore>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->treeWidget,SIGNAL(clicked(QModelIndex)),this,SLOT(changeText(QModelIndex)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::changeText(const QModelIndex &index){
// qDebug() << index.data().toString() << index.column();
// ui->treeWidget->currentItem()->setText(index.column(),index.data().toString()+
// tr("\n(this is write)"));
// QMessageBox::information(this,tr("title"),index.data().toString(),QMessageBox::Ok);
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}