qt学习之qtableview

表类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();
}
添加表头
向表中行或列添加表头用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);//设置标签对齐方式
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/creatory/archive/2007/12/18/1946707.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值