Qwidget在获取焦点时会绘制焦点虚框,在使用样式表制定皮肤的情况下可能很不和谐。采用setFocusPolicy(Qt::NoFocus),或者在样式表中指定padding:0px 这种方法试了无效果。查了下资料,方法好像很多,对于TreeView可以用重写代理的办法:
class FileViewerDelegate : public QItemDelegate
{
Q_OBJECT
public:
FileViewerDelegate(QObject *parent = 0) : QItemDelegate(parent){}
virtual void drawFocus(QPainter *, const QStyleOptionViewItem &,
const QRect &) const
{
// do nothing, so as to remove the virtual selected rectangle above treeview item
}
};
主函数中:
FileViewerDelegate *fileViewerDelegate = new FileViewerDelegate(this);
treeView->setItemDelegate(fileViewerDelegate);
这样虚框就消除了。对于有check框的视图如TableView,还可以在Delegate中加入下面代码达到类似效果:
virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &option,
const QRect &rect, Qt::CheckState state) const {}
网上搜到还有下列方法应该也是可以的,没试过:
#include <qproxystyle>
class MyProxyStyle : public QProxyStyle
{
public:
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption * option,
QPainter * painter, const QWidget * widget = 0) const
{
if (PE_FrameFocusRect == element)
{
/// do not draw focus rectangle
}
else
{
QProxyStyle::drawPrimitive(element, option,painter, widget);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
qApp->setStyle(new MyProxyStyle);//添加style为这个工程
return a.exec();
}
</qproxystyle>
可以去掉所有部件的矩形点阵虚框,详细介绍:http://blog.csdn.net/love_gaohz/article/details/13021409#