4

我正在尝试创建一个在顶部QTableView嵌入的子类,以便在输入时过滤结果。QLineEdit我需要我的表具有与 normal 相同的 API QTableView,所以我想对它进行子类化,而不是对其进行子类化QWidget和添加 a QLineEditand QTableView

我想我可以重新实现paintEvent(QPaintEvent*),改变QPaintEvent'srect()以稍微低一点( a 的高度QLineEdit,所以它在它下面绘制)然后将它传递给QTableView::paintEvent(),但QPaintEvent参数只规定了需要重新绘制的区域,而不是应该绘制小部件的区域。

4

3 回答 3

7

Anything you do in this regard is going to be hacky and result in just as much work (probably more work) as manually mapping all of the signals and slots to a child widget. You'll need to do a lot more than just change the paint events, you'd also have to adjust all of the mouse events, adjust any update rectangles, etc.

Alternatively you could just take the QTableView class from the Qt source and modify it directly (though that will probably break the LGPL and require you to publish your source if you don't have a commercial license.) But the easiest clean method is going to be implementing a container widget with the QTableView as a child.

于 2009-06-13T21:33:57.667 回答
1

我必须同意丹尼尔的观点:我认为这不是正确的做法。您可能希望创建一个带有行编辑的自定义小部件来执行过滤。否则,您将进入充满挑战的 Qt 黑客世界。

如果您确实需要提供对 QTableView 接口的访问,那么只需添加一个公共 get 方法,该方法返回对表的引用。

这有点类似于 Qt 提供 QTabWidget 类的方式,该类继承了 QWidget 但有一个内部使用的私有 QTabBar。一个显着的区别是它提供了一个受保护的tabBar() 访问器而不是一个公共访问器。

于 2009-06-17T16:40:18.230 回答
1

我会尝试覆盖paintEvent,将其更改为widget::pos低于它并调用QTableView::paintEvent()

于 2009-06-13T20:59:00.903 回答