自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在 Qt學(xué)習(xí) QTableItem 類

移動(dòng)開發(fā)
QTableItem類為QTable單元格提供內(nèi)容。在很多應(yīng)用中,QTableItem都適合于顯示和編輯QTable單元格。而在需要生成很大的表格的情況下,你可能更喜歡另一種方法,而不是使用QTableItem。

Qt類是一個(gè)提供所需的像全局變量一樣的大量不同的標(biāo)識(shí)符的命名空間。本篇文章介紹的是QTableItem類的使用。

QTableItem包含著一個(gè)單元格的數(shù)據(jù),缺省情況下是一個(gè)字符串和一個(gè)象素映射。表項(xiàng)還包括單元格的顯示大小和數(shù)據(jù)對(duì)齊方式,同時(shí)指定了單元格的EditType和用于內(nèi)嵌編輯的編輯器(缺省為QLineEdit)。如果你需要多選框,使用QCheckTableItem;需要組合框則使用QComboTableItem。EditType(在構(gòu)造器中設(shè)置的)決定單元格的內(nèi)容是否可以被編輯;setReplaceable()設(shè)置了單元格是否可以被另外一個(gè)單元格的內(nèi)容所替換。

如果指定了象素映射,總是顯示在文本在左邊??梢苑謩e用setText()和setPixmap()來改變文本或者象素映射。對(duì)于文本可以使用setWordWrap()。表項(xiàng)的對(duì)齊方式在構(gòu)造器中設(shè)置。

如果你想用自己的部件而不是QLineEdit來編輯單元格內(nèi)容,那就重新實(shí)現(xiàn)createEditor()和setContentFromEditor()。如果要顯示定制的內(nèi)容,就重寫paint()。

對(duì)表項(xiàng)排序使用了key()函數(shù);缺省情況下返回表項(xiàng)的文本()。重寫key()以定制你的表項(xiàng)排序方式。

使用QTable::setItem()把表項(xiàng)插入到表格中。如果你把表項(xiàng)插入到一個(gè)已經(jīng)有表項(xiàng)的單元格中,原有的表項(xiàng)被刪去。

例子:

  1. for ( int row = 0; row < table->numRows(); row++ ) {  
  2.        for ( int col = 0; col < table->numCols(); col++ ) {  
  3.            table->setItem( row, col,  
  4.                new QTableItem( table, WhenCurrent, QString::number( row * col ) ) );  
  5.        }  
  6.    } 

如果要在相同或者不同的表格中把表項(xiàng)從一個(gè)單元格移到另一個(gè),可以使用QTable::takeItem()和QTable::setItem(),但是也可以參見QTable::swapCells()。

表項(xiàng)可以以標(biāo)準(zhǔn)刪除的方式來刪去;表格和單元格將會(huì)相應(yīng)地更新。

qt類學(xué)習(xí)

表類QTableView用于創(chuàng)建表,頭文件<qtableview.h>,使用該類時(shí)必須重寫paintCell()函數(shù),該函數(shù)用于繪制表元,在QTableView中為虛函數(shù).實(shí)現(xiàn)paintCell()函數(shù),必然要用于QPainter類繪制圖形QPainter類,頭文件<qpainter.h>

創(chuàng)建簡單網(wǎng)格

  1. #include <qapplication.h> 
  2. #include <qwidget.h> 
  3. #include <qtableview.h> 
  4. #include <qpainter.h> 
  5. class MainWin:public QTableView  
  6. {  
  7. public:  
  8. MainWin();  
  9. private:  
  10. void paintCell(QPainter *,int ,int);  
  11. };  
  12. MainWin::MainWin()  
  13. {  
  14. setGeometry(100,100,300,300);  
  15. setNumCols(6);//設(shè)置表列數(shù)  
  16. setNumRows(10);//設(shè)置表行數(shù)  
  17. setCellWidth(50);//設(shè)置單元格寬度  
  18. setCellHeight(30);//設(shè)置單元格高度  
  19. }  
  20. //由QTableView自動(dòng)設(shè)置QPainter的繪圖區(qū)域  
  21. void MainWin::paintCell(QPainter *p,int row,int col)  
  22. {  
  23. int x=cellWidth(col);  
  24. int y=cellHeight(row);  
  25. p->drawLine(x,0,x,y);  
  26. p->drawLine(0,y,x,y);  
  27. }  
  28. int main(int argc,char **argv)  
  29. {  
  30. QApplication a(argc,argv);  
  31. MainWin w;  
  32. a.setMainWidget(&w);  
  33. w.show();  
  34. return a.exec();  

向表中添加文本和點(diǎn)擊選擇功能

  1. #include <qapplication.h> 
  2. #include <qwidget.h> 
  3. #include <qtableview.h> 
  4. #include <qpainter.h> 
  5. class MainWin:public QWidget  
  6. {  
  7. public:  
  8. MainWin();  
  9. private:  
  10. void paintCell(QPainter*,int,int);  
  11. void mousePressEvent(QMouseEvent*);  
  12. int curRow,curCol;  
  13. };  
  14. MainWin::MainWin()  
  15. {  
  16. setGeometry(100,100,300,300);  
  17. setNumCols(12);  
  18. setNumRows(20);  
  19. setCellWidth(80);  
  20. setCellHeight(30);  
  21. setTableFlags(Tbl_vScrollBar|Tbl_hScrollBar);//設(shè)置滾動(dòng)條  
  22. setBackgroundMode(PaletteBase);//設(shè)置背顏色  
  23. curRow=curCol=0;  
  24. }  
  25. void MainWin::paintCell(QPainter*p,int row,int col)  
  26. {  
  27. int x=(cellWidth(col)-1);  
  28. int y=(cellHeight(row)-1);  
  29. p->drawLine(x,0,x,y);  
  30. p->drawLine(0,y,x,y);  
  31. p->drawText(0,0,(x+1),(y+1),AlignCenter,"QT");  
  32. if((row==curRow)&&(col==curCol))  
  33. {  
  34. if(hasFocus())  
  35. {  
  36. p->drawRect(0,0,x,y);  
  37. }  
  38. else  
  39. {  
  40. p->setPen(DotLine);  
  41. p->drawRect(0,0,x,y);  
  42. p->setPen(SolidLine);  
  43. }  
  44. }  
  45. }  
  46. void MainWin::mousePressEvent(QMouseEvent*e)  
  47. {  
  48. int oldRow=curRow;  
  49. int oldCol=curCol;  
  50. QPoint clickedpos=e->pos();  
  51. curRow=findRow(clickedpos.y());  
  52. curCol=findCol(clickedpos.x());  
  53. if((curRow!=oldRow)||(curCol!=oldCol))  
  54. {  
  55. updateCell(oldRow,oldCol);  
  56. updateCell(curRow,curCol);  
  57. }  
  58. }  
  59.  
  60. int main(int argc,char **argv)  
  61. {  
  62. QApplication a(argc,argv);  
  63. MainWin w;  
  64. a.setMainWidget(&w);  
  65. w.show();  
  66. return a.exec();  

#p#

添加表頭,向表中行或列添加表頭用QHeader類實(shí)現(xiàn),頭文件<qheader.h>

  1. #include <qapplication.h> 
  2. #include <qwidget.h> 
  3. #include <qtableview.h> 
  4. #include <qpainter.h> 
  5. #include <qheader.h> 
  6. #include <qlabel.h> 
  7. class MyTable:public QTableView  
  8. {  
  9. public:  
  10. MyTable(QWidget *parent=0);  
  11. private:  
  12. void paintCell(QPainter*,int,int);  
  13. };  
  14. MyTable::MyTable(QWidget*parent):QTableView(parent)  
  15. {  
  16. setNumCols(5);  
  17. setNumRows(5);  
  18. setCellWidth(100);  
  19. setCellHeight(30);  
  20. setBackgroundMode(PaletteBase);  
  21. }  
  22. void MyTable::paintCell(QPainter*p,int row,int col)  
  23. {  
  24. int x=(cellWidth(col)-1);  
  25. int y=(cellHeight(row)-1);  
  26. p->drawLine(x,0,x,y);  
  27. p->drawLine(0,y,x,y);  
  28. if(col==0)  
  29. p->drawText(0,0,(x+1),(y+1),AlignCenter,"Name");  
  30. if(col==1)  
  31. p->drawText(0,0,(x+1),(y+1),AlignCenter,"Address");  
  32. if(col==2)  
  33. p->drawText(0,0,(x+1),(y+1),AlignCenter,"City");  
  34. if(col==3)  
  35. p->drawText(0,0,(x+1),(y+1),AlignCenter,"Gender");  
  36. if(col==4)  
  37. p->drawText(0,0,(x+1),(y+1),AlignCenter,"Tel.");  
  38. }  
  39. class MainWin:public QWidget  
  40. {  
  41. public:  
  42. MainWin();  
  43. private:  
  44. MyTable *table;  
  45. QHeader *header;  
  46. QLabel *label;  
  47. };  
  48. MainWin::MainWin()  
  49. {  
  50. resize(500,250);  
  51. table=new MyTable(this);  
  52. table->setGeometry(0,100,500,150);  
  53. header=new QHeader(this);  
  54. header->setGeometry(0,70,500,30);  
  55. header->setOrientation(Horizontal);  
  56. header->addLabel("name",100);  
  57. header->addLabel("address",100);  
  58. header->addLabel("city",100);  
  59. header->addLabel("gender",100);  
  60. header->addLabel("tel.",100);  
  61. label=new QLabel(this);  
  62. label->setGeometry(0,0,500,70);  
  63. label->setAlignment(AlignCenter);  
  64. label->setText("Let's pretend this is a real program that needs to present personal information in a table.");  
  65. }  
  66. int main(int argc,char **argv)  
  67. {  
  68. QApplication a(argc,argv);  
  69. MainWin w;  
  70. a.setMainWidget(&w);  
  71. w.show();  
  72. return a.exec();  

列表框部件QListBox,頭文件<qlistbox.h>,用于從中選擇一個(gè)或多個(gè)條目,可以為文本或位圖

  1. #include <qapplication.h> 
  2. #include <qwidget.h> 
  3. #include <qlistbox.h> 
  4. class MainWin:public QWidget  
  5. {  
  6. public:  
  7. MainWin();  
  8. private:  
  9. QListBox *listbox;  
  10. };  
  11. MainWin::MainWin()  
  12. {  
  13. setGeometry(100,100,170,100);  
  14. listbox=new QListBox(this);  
  15. listbox->setGeometry(10,10,150,80);  
  16. listbox->insertItem("Item1");  
  17. listbox->insertItem("Item2");  
  18. listbox->insertItem("Item3);  

主程序省略..

檢索當(dāng)前被選中位置QListBox::currentItem()

檢索指定位置的文本QListBox::text()/QListBox::pixmap()

組合框部件QComboBox,頭文件<qcombobox.h>

  1. #include <qcombobox.h> 
  2. class MainWin:pubilc QWidget  
  3. {  
  4. public:  
  5. MainWin();  
  6. private:  
  7. QComboBox *combobox;  
  8. };  
  9. MainWin::MainWin()  
  10. {  
  11. setGeometry(100,100,150,50);  
  12. combobox=new QComboBox(false,this);//創(chuàng)建組合框部件,***個(gè)參數(shù)指定讀寫屬性,true為可讀寫,false為只讀,第二個(gè)參數(shù)指定其父部件  
  13. combobox->setGeometry(10,10,130,30);  
  14. combobox->insertItem("Item1");  
  15. combobox->insertItem("Item2");  
  16. combobox->insertItem("Item3");  

部件布局類

QGroupBox
QButtonGroup
QSplitter
QWidgetStack
QFrame

分組框

QGroupBox用于在部件周圍繪制一個(gè)框架,頭文件<qgroupbox.h>

  1. #include <qgroupbox.h> 
  2. class MainWin:pubilc QWidget  
  3. {  
  4. public:  
  5. MainWin();  
  6. private:  
  7. QGroupBox *groupbox;  
  8. QLabel *label;  
  9. };  
  10. MainWin::MainWin()  
  11. {  
  12. setGeometry(100,100,150,100);  
  13. groupbox=new QGroupBox(this);  
  14. groupbox->setGeometry(10,10,130,80);  
  15. groupbox->setTitle("A Group Box");//設(shè)置分組框標(biāo)題  
  16.  
  17. label=new QLabel(this);  
  18. label->setGeometry(30,35,90,40);  
  19. lable->setText("Add Widgets\nhere!");//設(shè)置標(biāo)簽文本  
  20. label->setAlignment(AlignHCenter|AlignVCenter);//設(shè)置標(biāo)簽對(duì)齊方式  

小結(jié):在Qt學(xué)習(xí)QTableItem類的內(nèi)容就介紹到這,其實(shí)QTableItem類為QTable單元格提供內(nèi)容。在很多應(yīng)用中,QTableItem都適合于顯示和編輯QTable單元格。而在需要生成很大的表格的情況下,你可能更喜歡另一種方法,而不是使用QTableItem。

【編輯推薦】

新手須知 QT類大全

淺談Qt中多線程編程

QT中關(guān)于信號(hào)與槽機(jī)制的實(shí)現(xiàn)原理

利用Qt繪圖實(shí)現(xiàn)QWT繪制科學(xué)圖表

Qt類中配置文件的讀取之QSettings類

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-30 11:23:29

Qt 線程

2011-07-04 16:20:54

QT 窗口 QWidget

2011-06-16 11:28:48

Qt QApplicati

2011-07-02 13:24:39

QT Linux

2011-06-14 15:45:02

Qt Object

2011-06-16 11:04:07

Qt

2011-06-14 15:28:44

QT

2011-07-04 16:12:00

QT QWidget

2011-07-04 11:29:40

QT Designer

2011-07-04 11:21:59

QT Designer

2011-06-16 11:13:13

QtQWidget

2011-06-22 15:24:50

Qt 線程

2011-07-04 14:00:11

QT QEvent

2011-08-30 15:32:08

QtQuickQML

2011-06-17 10:19:11

Qt QWidge QSetting

2011-06-17 09:58:26

Qt Chapter QObject

2011-06-15 18:38:17

Linux Qt Symbian

2011-06-24 14:17:58

Qt 容器類 QVector

2011-06-24 15:06:40

QT

2011-06-30 16:38:07

Qt QTableWidg
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)