在 Qt學(xué)習(xí) 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)被刪去。
例子:
- 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 ) ) );
- }
- }
如果要在相同或者不同的表格中把表項(xiàng)從一個(gè)單元格移到另一個(gè),可以使用QTable::takeItem()和QTable::setItem(),但是也可以參見QTable::swapCells()。
表項(xiàng)可以以標(biāo)準(zhǔn)刪除的方式來刪去;表格和單元格將會(huì)相應(yīng)地更新。
表類QTableView用于創(chuàng)建表,頭文件<qtableview.h>,使用該類時(shí)必須重寫paintCell()函數(shù),該函數(shù)用于繪制表元,在QTableView中為虛函數(shù).實(shí)現(xiàn)paintCell()函數(shù),必然要用于QPainter類繪制圖形QPainter類,頭文件<qpainter.h>
創(chuàng)建簡單網(wǎng)格
- #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);//設(shè)置表列數(shù)
- setNumRows(10);//設(shè)置表行數(shù)
- setCellWidth(50);//設(shè)置單元格寬度
- setCellHeight(30);//設(shè)置單元格高度
- }
- //由QTableView自動(dòng)設(shè)置QPainter的繪圖區(qū)域
- 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();
- }
向表中添加文本和點(diǎn)擊選擇功能
- #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);//設(shè)置滾動(dòng)條
- setBackgroundMode(PaletteBase);//設(shè)置背顏色
- 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類實(shí)現(xiàn),頭文件<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>,用于從中選擇一個(gè)或多個(gè)條目,可以為文本或位圖
- #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);
- }
主程序省略..
檢索當(dāng)前被選中位置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);//創(chuàng)建組合框部件,***個(gè)參數(shù)指定讀寫屬性,true為可讀寫,false為只讀,第二個(gè)參數(shù)指定其父部件
- combobox->setGeometry(10,10,130,30);
- combobox->insertItem("Item1");
- combobox->insertItem("Item2");
- combobox->insertItem("Item3");
- }
部件布局類
QGroupBox
QButtonGroup
QSplitter
QWidgetStack
QFrame
分組框
QGroupBox用于在部件周圍繪制一個(gè)框架,頭文件<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");//設(shè)置分組框標(biāo)題
- label=new QLabel(this);
- label->setGeometry(30,35,90,40);
- lable->setText("Add Widgets\nhere!");//設(shè)置標(biāo)簽文本
- 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中關(guān)于信號(hào)與槽機(jī)制的實(shí)現(xiàn)原理