01. 字符串的处理
QString基本上和String是相同的
QString contains a split() method that takes the separator string as its parameter and returns a list of string that are represented in Qt by the QStringList class.
Methods such as toInt(), toDouble(), or toLongLong() make it easy to extract numberical values from strings.
02.第四章比较晦涩,一些字符串的处理,以及保存一些东西,以后需要的时候在学习。
现在遵循的宗旨是看Qt,看不懂的就跳过,再看不懂的再跳过。
直到看懂为止,努力学习,结合官网。
Chapter 5: Graphics with Qt
03.主要学会的知识
绘制2D和3D图形,
不怎会会,要求有点高。
贴上源码
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = customwidget
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
Q_PROPERTY(int selectionStart READ selectionStart NOTIFY selectionChanged)
Q_PROPERTY(int selectionEnd READ selectionEnd NOTIFY selectionChanged)
public:
Widget(QWidget *parent = 0);
~Widget();
int selectionStart() const
{
return m_selectionStart;
}
int selectionEnd() const
{
return m_selectionEnd;
}
public slots:
void addPoint(unsigned yVal) { m_points << qMax(0u, yVal); update(); }
void clear() { m_points.clear(); update(); }
signals:
void selectionChanged();
protected:
void paintEvent(QPaintEvent *);
void drawChart(QPainter *painter, const QRect &rect, const QRect &exposedRect);
void drawSelection(QPainter *painter, const QRect &rect, const QRect &exposedRect);
void mousePressEvent(QMouseEvent *mouseEvent);
void mouseMoveEvent(QMouseEvent *mouseEvent);
QList<quint16> m_points;
int m_selectionStart;
int m_selectionEnd;
};
#endif // WIDGET_H
#include "widget.h"
#include <QMouseEvent>
#include <QPainter>
Widget::Widget(QWidget *parent)
: QWidget(parent), m_selectionStart(-1), m_selectionEnd(-1)
{
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent *pe)
{
QRect exposedRect = pe->rect();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
QPen pen(Qt::black);
pen.setWidth(4);
painter.setPen(pen);
QRect r = rect().adjusted(10, 10, -10, -10);
painter.drawRoundedRect(r, 20, 10);
painter.save();
r.adjust(2, 2, -2, -2);
painter.setViewport(r);
r.moveTo(0, -r.height()/2);
painter.setWindow(r);
drawSelection(&painter, r, exposedRect);
drawChart(&painter, r, exposedRect);
painter.restore();
}
void Widget::drawChart(QPainter *painter, const QRect &, const QRect &exposedRect)
{
painter->setPen(Qt::red);
painter->drawLine(exposedRect.left(), 0, exposedRect.width(), 0);
painter->save();
painter->setRenderHint(QPainter::Antialiasing, false);
const int lastPoint = qMin(m_points.size(), exposedRect.right()+1);
for(int i=exposedRect.left(); i < lastPoint; ++i) {
if(m_selectionStart <= i && m_selectionEnd >=i) {
painter->setPen(Qt::white);
} else
painter->setPen(Qt::blue);
painter->drawLine(i, -m_points.at(i), i, m_points.at(i));
}
painter->restore();
}
void Widget::mousePressEvent(QMouseEvent *mouseEvent) {
m_selectionStart = m_selectionEnd = mouseEvent->pos().x() - 12;
emit selectionChanged();
update();
}
void Widget::mouseMoveEvent(QMouseEvent *mouseEvent) {
m_selectionEnd = mouseEvent->pos().x() - 12;
emit selectionChanged();
update();
}
void Widget::drawSelection(QPainter *painter, const QRect &rect, const QRect &exposedRect) {
if(m_selectionStart < 0 ) return;
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(palette().highlight());
QRect selectionRect = rect;
selectionRect.setLeft(m_selectionStart);
selectionRect.setRight(m_selectionEnd);
painter->drawRect(selectionRect.intersected(exposedRect));
painter->restore();
}
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
for(int i=0;i<450;++i) w.addPoint(qrand() % 120);
w.show();
return a.exec();
}
04. 绘制一个棋盘
国际象棋的走法是什么呢?
需要理解国际象棋的下法,然后写出相应的源码出来吗?
那就下写中国想起怎么样?
可是不会写啊!
Game Programming Using QT
非常好的一本书。
05. opengl qt中使用
总结:
In this chapter, we learn about using graphics with Qt. You should be aware we have only scratched the surface of Qt capabilities in this regard. What was presented in this chapter will let you implement custom widgets, do some basic painting on images, and render OpenGL scenes. There are many more functionalities that we didn't go through, such as composition modes, paths, SVG handling, and many others. We will come back to some of these features in subsequent chapters, but we will leave most for you to discover on your own.
Chapter 6: Graphics View