在 Qt學習 QTableItem 類
Qt類是一個提供所需的像全局變量一樣的大量不同的標識符的命名空間。本篇文章介紹的是QTableItem類的使用。
QTableItem包含著一個單元格的數據,缺省情況下是一個字符串和一個象素映射。表項還包括單元格的顯示大小和數據對齊方式,同時指定了單元格的EditType和用于內嵌編輯的編輯器(缺省為QLineEdit)。如果你需要多選框,使用QCheckTableItem;需要組合框則使用QComboTableItem。EditType(在構造器中設置的)決定單元格的內容是否可以被編輯;setReplaceable()設置了單元格是否可以被另外一個單元格的內容所替換。
如果指定了象素映射,總是顯示在文本在左邊。可以分別用setText()和setPixmap()來改變文本或者象素映射。對于文本可以使用setWordWrap()。表項的對齊方式在構造器中設置。
如果你想用自己的部件而不是QLineEdit來編輯單元格內容,那就重新實現createEditor()和setContentFromEditor()。如果要顯示定制的內容,就重寫paint()。
對表項排序使用了key()函數;缺省情況下返回表項的文本()。重寫key()以定制你的表項排序方式。
使用QTable::setItem()把表項插入到表格中。如果你把表項插入到一個已經有表項的單元格中,原有的表項被刪去。
例子:
- for ( int row = 0; row < table->numRows(); row++ ) {
- for ( int col = 0; col < table->numCols(); col++ ) {
- table->setItem( row, col,
- new QTableItem( table, WhenCurrent, QString::number( row * col ) ) );
- }
- }
如果要在相同或者不同的表格中把表項從一個單元格移到另一個,可以使用QTable::takeItem()和QTable::setItem(),但是也可以參見QTable::swapCells()。
表項可以以標準刪除的方式來刪去;表格和單元格將會相應地更新。
表類QTableView用于創建表,頭文件<qtableview.h>,使用該類時必須重寫paintCell()函數,該函數用于繪制表元,在QTableView中為虛函數.實現paintCell()函數,必然要用于QPainter類繪制圖形QPainter類,頭文件<qpainter.h>
創建簡單網格
- #include <qapplication.h>
- #include <qwidget.h>
- #include <qtableview.h>
- #include <qpainter.h>
- class MainWin:public QTableView
- {
- public:
- MainWin();
- private:
- void paintCell(QPainter *,int ,int);
- };
- MainWin::MainWin()
- {
- setGeometry(100,100,300,300);
- setNumCols(6);//設置表列數
- setNumRows(10);//設置表行數
- setCellWidth(50);//設置單元格寬度
- setCellHeight(30);//設置單元格高度
- }
- //由QTableView自動設置QPainter的繪圖區域
- void MainWin::paintCell(QPainter *p,int row,int col)
- {
- int x=cellWidth(col);
- int y=cellHeight(row);
- p->drawLine(x,0,x,y);
- p->drawLine(0,y,x,y);
- }
- int main(int argc,char **argv)
- {
- QApplication a(argc,argv);
- MainWin w;
- a.setMainWidget(&w);
- w.show();
- return a.exec();
- }
向表中添加文本和點擊選擇功能
- #include <qapplication.h>
- #include <qwidget.h>
- #include <qtableview.h>
- #include <qpainter.h>
- class MainWin:public QWidget
- {
- public:
- MainWin();
- private:
- void paintCell(QPainter*,int,int);
- void mousePressEvent(QMouseEvent*);
- int curRow,curCol;
- };
- MainWin::MainWin()
- {
- setGeometry(100,100,300,300);
- setNumCols(12);
- setNumRows(20);
- setCellWidth(80);
- setCellHeight(30);
- setTableFlags(Tbl_vScrollBar|Tbl_hScrollBar);//設置滾動條
- setBackgroundMode(PaletteBase);//設置背顏色
- curRow=curCol=0;
- }
- void MainWin::paintCell(QPainter*p,int row,int col)
- {
- int x=(cellWidth(col)-1);
- int y=(cellHeight(row)-1);
- p->drawLine(x,0,x,y);
- p->drawLine(0,y,x,y);
- p->drawText(0,0,(x+1),(y+1),AlignCenter,"QT");
- if((row==curRow)&&(col==curCol))
- {
- if(hasFocus())
- {
- p->drawRect(0,0,x,y);
- }
- else
- {
- p->setPen(DotLine);
- p->drawRect(0,0,x,y);
- p->setPen(SolidLine);
- }
- }
- }
- void MainWin::mousePressEvent(QMouseEvent*e)
- {
- int oldRow=curRow;
- int oldCol=curCol;
- QPoint clickedpos=e->pos();
- curRow=findRow(clickedpos.y());
- curCol=findCol(clickedpos.x());
- if((curRow!=oldRow)||(curCol!=oldCol))
- {
- updateCell(oldRow,oldCol);
- updateCell(curRow,curCol);
- }
- }
- int main(int argc,char **argv)
- {
- QApplication a(argc,argv);
- MainWin w;
- a.setMainWidget(&w);
- w.show();
- return a.exec();
- }
#p#
添加表頭,向表中行或列添加表頭用QHeader類實現,頭文件<qheader.h>
- #include <qapplication.h>
- #include <qwidget.h>
- #include <qtableview.h>
- #include <qpainter.h>
- #include <qheader.h>
- #include <qlabel.h>
- class MyTable:public QTableView
- {
- public:
- MyTable(QWidget *parent=0);
- private:
- void paintCell(QPainter*,int,int);
- };
- MyTable::MyTable(QWidget*parent):QTableView(parent)
- {
- setNumCols(5);
- setNumRows(5);
- setCellWidth(100);
- setCellHeight(30);
- setBackgroundMode(PaletteBase);
- }
- void MyTable::paintCell(QPainter*p,int row,int col)
- {
- int x=(cellWidth(col)-1);
- int y=(cellHeight(row)-1);
- p->drawLine(x,0,x,y);
- p->drawLine(0,y,x,y);
- if(col==0)
- p->drawText(0,0,(x+1),(y+1),AlignCenter,"Name");
- if(col==1)
- p->drawText(0,0,(x+1),(y+1),AlignCenter,"Address");
- if(col==2)
- p->drawText(0,0,(x+1),(y+1),AlignCenter,"City");
- if(col==3)
- p->drawText(0,0,(x+1),(y+1),AlignCenter,"Gender");
- if(col==4)
- p->drawText(0,0,(x+1),(y+1),AlignCenter,"Tel.");
- }
- class MainWin:public QWidget
- {
- public:
- MainWin();
- private:
- MyTable *table;
- QHeader *header;
- QLabel *label;
- };
- MainWin::MainWin()
- {
- resize(500,250);
- table=new MyTable(this);
- table->setGeometry(0,100,500,150);
- header=new QHeader(this);
- header->setGeometry(0,70,500,30);
- header->setOrientation(Horizontal);
- header->addLabel("name",100);
- header->addLabel("address",100);
- header->addLabel("city",100);
- header->addLabel("gender",100);
- header->addLabel("tel.",100);
- label=new QLabel(this);
- label->setGeometry(0,0,500,70);
- label->setAlignment(AlignCenter);
- label->setText("Let's pretend this is a real program that needs to present personal information in a table.");
- }
- int main(int argc,char **argv)
- {
- QApplication a(argc,argv);
- MainWin w;
- a.setMainWidget(&w);
- w.show();
- return a.exec();
- }
列表框部件QListBox,頭文件<qlistbox.h>,用于從中選擇一個或多個條目,可以為文本或位圖
- #include <qapplication.h>
- #include <qwidget.h>
- #include <qlistbox.h>
- class MainWin:public QWidget
- {
- public:
- MainWin();
- private:
- QListBox *listbox;
- };
- MainWin::MainWin()
- {
- setGeometry(100,100,170,100);
- listbox=new QListBox(this);
- listbox->setGeometry(10,10,150,80);
- listbox->insertItem("Item1");
- listbox->insertItem("Item2");
- listbox->insertItem("Item3);
- }
主程序省略..
檢索當前被選中位置QListBox::currentItem()
檢索指定位置的文本QListBox::text()/QListBox::pixmap()
組合框部件QComboBox,頭文件<qcombobox.h>
- #include <qcombobox.h>
- class MainWin:pubilc QWidget
- {
- public:
- MainWin();
- private:
- QComboBox *combobox;
- };
- MainWin::MainWin()
- {
- setGeometry(100,100,150,50);
- combobox=new QComboBox(false,this);//創建組合框部件,***個參數指定讀寫屬性,true為可讀寫,false為只讀,第二個參數指定其父部件
- combobox->setGeometry(10,10,130,30);
- combobox->insertItem("Item1");
- combobox->insertItem("Item2");
- combobox->insertItem("Item3");
- }
部件布局類
QGroupBox
QButtonGroup
QSplitter
QWidgetStack
QFrame
分組框
QGroupBox用于在部件周圍繪制一個框架,頭文件<qgroupbox.h>
- #include <qgroupbox.h>
- class MainWin:pubilc QWidget
- {
- public:
- MainWin();
- private:
- QGroupBox *groupbox;
- QLabel *label;
- };
- MainWin::MainWin()
- {
- setGeometry(100,100,150,100);
- groupbox=new QGroupBox(this);
- groupbox->setGeometry(10,10,130,80);
- groupbox->setTitle("A Group Box");//設置分組框標題
- label=new QLabel(this);
- label->setGeometry(30,35,90,40);
- lable->setText("Add Widgets\nhere!");//設置標簽文本
- label->setAlignment(AlignHCenter|AlignVCenter);//設置標簽對齊方式
- }
小結:在Qt學習QTableItem類的內容就介紹到這,其實QTableItem類為QTable單元格提供內容。在很多應用中,QTableItem都適合于顯示和編輯QTable單元格。而在需要生成很大的表格的情況下,你可能更喜歡另一種方法,而不是使用QTableItem。
【編輯推薦】