Qt小总结

来源:互联网 发布:张松献图 知乎 编辑:程序博客网 时间:2024/05/19 13:20

学了那么久了,发现还有很多Qt功能还不会使用,特地从百度摘抄了一份: 

1> 定时器的使用  
QTimer *timer = new QTimer(this);  
connect(timer, SIGNAL(timeout()), this, SLOT(update())); // 设置定时器回调函数  timer->start(1000); // 启动定时器  

2> 得到系统当前时间  
QTime time = QTime::currentTime();  time.hour();  time.minute(); time.second();  time.msec();  

3> 窗口 widget 相关  
setWindowTitle(tr("My Title")); // 设置窗口标题  resize(200, 200); // 设置窗口大小 width();// 窗口宽度 height();// 窗口高度   

4> 窗口(widget) 事件回调相关 (具体参考 widget.h 文件)   
#ifndef QT_NO_WHEELEVENT  
virtual void wheelEvent(QWheelEvent *); #endif  
virtual void keyPressEvent(QKeyEvent *);  virtual void keyReleaseEvent(QKeyEvent *);  virtual void focusInEvent(QFocusEvent *);  virtual void focusOutEvent(QFocusEvent *);  
virtual void paintEvent(QPaintEvent *); // 每次刷新屏幕都会回调此函数

virtual void actionEvent(QActionEvent *);


5> QT 画布(QPainter)的使用

1. QColor hourColor(127, 0, 127); // 画笔颜色 2.  
3. QPainter painter(objWidget); // 创建一个画布, objWidget 为当前 画笔的要画到哪里, 一般为
 Widget 对象

 4. // 设置画布的样式 
5. painter.setPen(Qt::NoPen);

6. painter.setBrush(hourColor); 
7. painter.translate(width() / 2, height() / 2); // 画布坐标系统的移动(当前窗口的中心点位置) 

8. painter.scale(side / 200.0, side / 200.0); // 设置缩放 

9. painter.rotate(30.0); // 坐标系统,旋转 30 度 

10. painter.save(); 

11.  
12. painter.drawLine(92, 0, 96, 0); 

1.  
2. // 常用函数(参考QPainter.h) 

3. void drawText() 

4. void fillRect() 

5. void drawPoint() 
6. void drawPoints(const QPoint *points, int pointCount); 

7. void drawLine(int x1, int y1, int x2, int y2);

8. void drawLines(const QLineF *lines, int lineCount); 

9. void drawRect(int x1, int y1, int w, int h); 
10. void drawRects(const QRectF *rects, int rectCount); 

11. void drawEllipse(int x, int y, int w, int h); 
12. void drawPolyline(const QPointF *points, int pointCount); 

13.  
14. void drawText(const QPointF &p, const QString &s); 

15. void drawImage() // 可以绘制图片 

16.  
17. void eraseRect() 

18.  
19. void scale(qreal sx, qreal sy); // 设置缩放  

20. void rotate(qreal a);// 设置旋转 
21. void setBackground(const QBrush &bg); // 设置背景颜色 

22. void setPen(const QColor &color); // 设置画笔 

22. void setPen(const QColor &color); // 设置画笔 
 
1. // 画一个路径( QPainterPath 可以存储 painter 的路径信息) 

2. QPainterPath clock; // 初始化一个钟表的形状 

3. clock.addEllipse(-50.0, -50.0, 100.0, 100.0); 

4. clock.addEllipse(-48.0, -48.0, 96.0, 96.0); 

5. clock.moveTo(0.0, 0.0); 

6. clock.lineTo(-2.0, -2.0); 

7. clock.lineTo(0.0, -42.0); 

8. clock.lineTo(2.0, -2.0); 

9. clock.lineTo(0.0, 0.0); 

10. clock.moveTo(0.0, 0.0); 

11. clock.lineTo(2.732, -0.732); 

12. clock.lineTo(24.495, 14.142); 

13. clock.lineTo(0.732, 2.732); 

14. clock.lineTo(0.0, 0.0); 

15.  
16. QPainter painter(objWidget); // 创建一个画布, objWidget 为当前 画笔的要画到哪里, 一般为
 Widget 对象 

17.  
18. painter.fillPath(clock, Qt::blue); 
 
1. // 绘制一个文本 2.  
3. QPainterPath text;

 4. QFont font; 
5. font.setPixelSize(50); 
6. QRect fontBoundingRect = QFontMetrics(font).boundingRect(tr("Qt")); 

7. text.addText(-QPointF(fontBoundingRect.center()), font, tr("Qt")); 

8.  
9. QPainter painter(objWidget); // 创建一个画布, objWidget 为当前 画笔的要画到哪里, 一般为
 Widget 对象 
10. painter.fillPath(text, Qt::blue); // 绘制一个文本 
painter.drawText(100,100, tr("Hello!!!")); // 一句话也可以搞定 6> Layout 的使用举例  

1. QGridLayout *mainLayout = new QGridLayout;

2.  
3. // 2行 3列的一个网格布局 
4. for (int i = 0; i < 2; ++i) { // 行 

5. for (int j = 0; j < 3; ++j) { // 列 

6.  
7. glWidgets[i][j] = new GLWidget(0, 0); 

8. mainLayout->addWidget(glWidgets[i][j], i, j); // 直接将其他窗口对象,添加到 mainLayout 
中就可以 

9. } 

10. } 
11. setLayout(mainLayout); // 调用窗口对象的 setLayout 方法来设置布局 


7> QT 控件的使用 总结 
1. QComboBox 下拉选择控件 

2.  
3. shapeComboBox = new QComboBox; 

4. shapeComboBox->addItem(tr("Clock")); 

5. shapeComboBox->addItem(tr("House")); 

6. shapeComboBox->addItem(tr("Text")); 

7. shapeComboBox->addItem(tr("Truck")); 

8.  
9. // 置回调函数 operationChanged 
10. connect(shapeComboBox, SIGNAL(activated(int)), this, SLOT(shapeSelected(int))); 

11.  
12. // 回到函数中, index 为当前选择的索引 

13. void Window::shapeSelected(int index) 

14. { 

15. } 

16.  
17. int index = shapeComboBox->currentIndex(); // 可以得到当前选择的索引 
  
1. // Table View 的使用 

2.  
3. QStandardItemModel model(4, 2); // 设置表格的数据模型 ( 4 行 2 列) 

4. QTableView tableView; // 表格对象 
5. tableView.setModel(&model);// 设置数据模型 

6.  

7.  
8. // 设置表格对象的数据显示 
9. for (int row = 0; row < 10; ++row)  

10. { 
11. for (int column = 0; column < 5; ++column)  

12. { 
13. QModelIndex index = model.index(row, column, QModelIndex()); // 得到某一个具体的数据索
引 
14. model.setData(index, QVariant("aaaa")); // 设置数据 15. } 16. } 
表格数据的操作: 

1. model.setColumnCount(5); // 设置表格的列数 

2. model.setRowCount(2); // 设置表格的行数 
3. model.insertColumn(1); // 在第 1列后面,插入一列

4. model.insertRow(1); // 在第 1 行后面,插入一列 

5. model.rowCount(); // 表格行数

6. model.columnCount(); // 表格列数 




0 0
原创粉丝点击