成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

在 Qt學習 QTableItem 類

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

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

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

如果指定了象素映射,總是顯示在文本在左邊。可以分別用setText()和setPixmap()來改變文本或者象素映射。對于文本可以使用setWordWrap()。表項的對齊方式在構造器中設置。

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

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

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

例子:

  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.    } 

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

表項可以以標準刪除的方式來刪去;表格和單元格將會相應地更新。

qt類學習

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

創建簡單網格

  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);//設置表列數  
  16. setNumRows(10);//設置表行數  
  17. setCellWidth(50);//設置單元格寬度  
  18. setCellHeight(30);//設置單元格高度  
  19. }  
  20. //由QTableView自動設置QPainter的繪圖區域  
  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();  

向表中添加文本和點擊選擇功能

  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);//設置滾動條  
  22. setBackgroundMode(PaletteBase);//設置背顏色  
  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類實現,頭文件<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>,用于從中選擇一個或多個條目,可以為文本或位圖

  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);  

主程序省略..

檢索當前被選中位置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);//創建組合框部件,***個參數指定讀寫屬性,true為可讀寫,false為只讀,第二個參數指定其父部件  
  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用于在部件周圍繪制一個框架,頭文件<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");//設置分組框標題  
  16.  
  17. label=new QLabel(this);  
  18. label->setGeometry(30,35,90,40);  
  19. lable->setText("Add Widgets\nhere!");//設置標簽文本  
  20. label->setAlignment(AlignHCenter|AlignVCenter);//設置標簽對齊方式  

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

【編輯推薦】

新手須知 QT類大全

淺談Qt中多線程編程

QT中關于信號與槽機制的實現原理

利用Qt繪圖實現QWT繪制科學圖表

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

責任編輯:zhaolei 來源: 互聯網
相關推薦

2011-06-30 11:23:29

Qt 線程

2011-07-04 16:20:54

QT 窗口 QWidget

2011-07-02 13:24:39

QT Linux

2011-06-16 11:28:48

Qt QApplicati

2011-06-14 15:45:02

Qt Object

2011-07-04 11:29:40

QT Designer

2011-07-04 11:21:59

QT Designer

2011-06-16 11:04:07

Qt

2011-06-14 15:28:44

QT

2011-07-04 16:12:00

QT QWidget

2011-06-16 11:13:13

QtQWidget

2011-06-15 18:38:17

Linux Qt Symbian

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-22 15:24:50

Qt 線程

2011-07-04 14:00:11

QT QEvent

2011-06-13 17:46:07

Qt 串口通信

2011-06-15 10:08:01

Qt CVS

2011-06-13 15:57:26

linux QT QTOPIA
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: h片在线看 | 中日韩欧美一级片 | 国产一区二区三区在线观看免费 | 亚洲免费人成在线视频观看 | www.xxxx欧美| 日韩精品免费一区 | 欧美激情精品久久久久 | 操人网站 | 精品视频导航 | 日韩精品一区二区三区 | 在线播放一区二区三区 | 精品久久久久久久久久 | 欧美精品在线一区 | 国产一级免费在线观看 | www在线视频 | 欧美在线成人影院 | 在线播放日韩 | 亚洲品质自拍视频网站 | 欧美视频在线播放 | 国产精品久久久久久久久久久久久 | 成人精品一区二区三区中文字幕 | 国产福利资源在线 | 国产美女h视频 | 国产特级毛片 | 欧美日韩一区在线 | 免费观看一级视频 | 亚洲综合二区 | 视频在线一区二区 | 国产午夜精品一区二区三区嫩草 | 午夜丰满少妇一级毛片 | 国产精品入口久久 | 国产一区欧美一区 | 国产精品一区二区av | 中文字幕乱码一区二区三区 | 神马福利 | 日韩欧美国产精品一区二区三区 | 免费午夜电影 | 久久99精品视频 | 日韩另类 | 久久久久久美女 | 久久99精品视频 |