1、界面设计
在如何设计出美观的页面方面,主要考虑使用setStyleSheet()函数,函数的参数是QString字符串。而里面是的qss语句和html中用到的css语句很相似。在界面设计这一块用setstylesheet()可以解决大部分问题,而对于list、Slider、progress等会动态变化的控件。setstylesheet()是解决不了问题的,目前我的做法是自定义自己的list、slider等控件。除了重写Paint函数外,就还会使用对于鼠标的捕获及响应。有个例子qt/demos/embedded/flickable,可以看看,就能知道如何自定义一个自己想要的控件(可以响应鼠标事件)。
这是我定义的一个list控件,不过要用到flickable接口,这个接口就自己到上面的目录去找了
#include "MyList.h" MyListview::MyListview(QWidget *parent): QWidget(parent) { m_offset = 0; m_height = QFontMetrics(font()).height() + 20; m_highlight = -1; m_selected = -1; m_parent = parent; textName << "Volume" << "Brightness" << "Language" << "About" << "hello1" << "hello2" << "hello3"; setAttribute(Qt::WA_OpaquePaintEvent, true); setAttribute(Qt::WA_NoSystemBackground, true); setMouseTracking(true); Flickable::setAcceptMouseClick(this); } // reimplement from Flickable QPoint MyListview::scrollOffset() const { return QPoint(0, m_offset); } // reimplement from Flickable void MyListview::setScrollOffset(const QPoint &offset) { int yy = offset.y(); if (yy != m_offset) { m_offset = yy; emit mousemove(-m_offset); } } void MyListview::paintEvent(QPaintEvent *event) { QPainter p(this); p.fillRect(event->rect(), Qt::gray); QFont font; font.setPointSize(14); font.setBold(true); font.setFamily(QString::fromUtf8("Arial")); p.setFont(font); int start = 0; int y = 0; int end = start + height() / m_height + 1; if (end > textName.count() - 1) end = textName.count() - 1; for (int i = start; i <= end; ++i, y += m_height) { p.setBrush(Qt::NoBrush); p.setPen(Qt::black); if (i == m_highlight) { p.fillRect(0, y, width(), m_height, QColor(0, 64, 128)); p.setPen(Qt::white); } if (i == m_selected) { p.fillRect(0, y, width(), m_height, QColor(170, 170, 170)); p.setPen(Qt::white); } p.fillRect(0, y + m_height - 1, width(), 1, QColor(255, 255, 255)); p.drawText(m_height + 2, y, width(), m_height, Qt::AlignVCenter, textName[i]); } p.end(); } void MyListview::mousePressEvent(QMouseEvent *event) { Flickable::handleMousePress(event); if (event->isAccepted()) return; if (event->button() == Qt::LeftButton) { int y = event->pos().y() + m_offset; int i = y / m_height; if (i != m_highlight) { m_highlight = i; m_selected = -1; update(); } event->accept(); } } void MyListview::mouseMoveEvent(QMouseEvent *event) { Flickable::handleMouseMove(event); } void MyListview::mouseReleaseEvent(QMouseEvent *event) { Flickable::handleMouseRelease(event); if (event->isAccepted()) return; if (event->button() == Qt::LeftButton) { m_selected = m_highlight; event->accept(); update(); emit listclick(m_selected); } } int MyListview::getm_height() { return m_height; }
2、下层功能实现实现
功能实现中我们可以使用windows API实现我们的系统的操作。而且QT中还封装了很多的类给我们使用,比如对注册表的操作(QSettings),对XML文件的操作(QDomDocument、QXMLStreamReader),QFile等等。QT的资源是很丰富的了。信号与槽机制也大大的降低了在VS中自己处理消息循环的麻烦。
3、调试比较方便