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 numRows(); row++ ) {
for ( int col = 0; col numCols(); col++ ) {
table->setItem( row, col,
new QTableItem( table, WhenCurrent, QString::number( row * col ) ) );
}
}
如果要在相同或者不同的表格中把表项从一个单元格移到另一个,可以使用QTable::takeItem()和QTable::setItem(),但是也可以参见QTable::swapCells()。
表项可以以标准删除的方式来删去;表格和单元格将会相应地更新。
表类QTableView用于创建表,头文件,使用该类时必须重写paintCell()函数,该函数用于绘制表元,在QTableView中为虚函数.实现paintCell()函数,必然要用于QPainter类绘制图形QPainter类,头文件
创建简单网格
#include
#include
#include
#include
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)
{
intx=cellWidth(col);
inty=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
#include
#include
#include
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)
{
intx=(cellWidth(col)-1);
inty=(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)
{
intoldRow=curRow;
intoldCol=curCol;
QPointclickedpos=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();
}
添加表头,向表中行或列添加表头用QHeader类实现,头文件
#include
#include
#include
#include
#include
#include
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)
{
intx=(cellWidth(col)-1);
inty=(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=newMyTable(this);
table->setGeometry(0,100,500,150);
header=newQHeader(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=newQLabel(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,头文件,用于从中选择一个或多个条目,可以为文本或位图
#include
#include
#include
class MainWin:public QWidget
{
public:
MainWin();
private:
QListBox *listbox;
};
MainWin::MainWin()
{
setGeometry(100,100,170,100);
listbox=newQListBox(this);
listbox->setGeometry(10,10,150,80);
listbox->insertItem("Item1");
listbox->insertItem("Item2");
listbox->insertItem("Item3);
}
主程序省略..
检索当前被选中位置QListBox::currentItem()
检索指定位置的文本QListBox::text()/QListBox::pixmap()
组合框部件QComboBox,头文件
#include
class MainWin:pubilc QWidget
{
public:
MainWin();
private:
QComboBox *combobox;
};
MainWin::MainWin()
{
setGeometry(100,100,150,50);
combobox=newQComboBox(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用于在部件周围绘制一个框架,头文件
#include
class MainWin:pubilc QWidget
{
public:
MainWin();
private:
QGroupBox *groupbox;
QLabel *label;
};
MainWin::MainWin()
{
setGeometry(100,100,150,100);
groupbox=newQGroupBox(this);
groupbox->setGeometry(10,10,130,80);
groupbox->setTitle("A Group Box");//设置分组框标题
label=newQLabel(this);
label->setGeometry(30,35,90,40);
lable->setText("Add Widgets\nhere!");//设置标签文本
label->setAlignment(AlignHCenter|AlignVCenter);//设置标签对齐方式
}
小结:在Qt学习QTableItem类的内容就介绍到这,其实QTableItem类为QTable单元格提供内容。在很多应用中,QTableItem都适合于显示和编辑QTable单元格。而在需要生成很大的表格的情况下,你可能更喜欢另一种方法,而不是使用QTableItem。
【编辑推荐】
【责任编辑:李程站 TEL:(010)68476606】
点赞 0