如何解决如何以编程方式在Qt中绘制水平线
| 我试图弄清楚如何在Qt中制作一条水平线。这在Designer中很容易创建,但是我想以编程方式创建一个。我已经完成了一些Google搜索工作,并查看了ui文件中的xml,但还没有发现任何问题。 这是ui文件中xml的样子: <widget class=\"Line\" name=\"line\">
<property name=\"geometry\">
<rect>
<x>150</x>
<y>110</y>
<width>118</width>
<height>3</height>
</rect>
</property>
<property name=\"orientation\">
<enum>Qt::Horizontal</enum>
</property>
</widget>
解决方法
水平或垂直线只是设置了某些属性的
QFrame
。在C ++中,用于创建行的代码如下所示:
line = new QFrame(w);
line->setObjectName(QString::fromUtf8(\"line\"));
line->setGeometry(QRect(320,150,118,3));
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
,这是使用PySide的另一种解决方案:
from PySide.QtGui import QFrame
class QHLine(QFrame):
def __init__(self):
super(QHLine,self).__init__()
self.setFrameShape(QFrame.HLine)
self.setFrameShadow(QFrame.Sunken)
class QVLine(QFrame):
def __init__(self):
super(QVLine,self).__init__()
self.setFrameShape(QFrame.VLine)
self.setFrameShadow(QFrame.Sunken)
然后可以用作(例如):
from PySide.QtGui import QApplication,QWidget,QGridLayout,QLabel,QComboBox
if __name__ == \"__main__\":
app = QApplication([])
widget = QWidget()
layout = QGridLayout()
layout.addWidget(QLabel(\"Test 1\"),1,1)
layout.addWidget(QComboBox(),1)
layout.addWidget(QHLine(),2)
layout.addWidget(QLabel(\"Test 2\"),2,1)
widget.setLayout(layout)
widget.show()
app.exec_()
结果如下:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。