QMenuで、QActionを表示したいのですが、
テキストやアイコンではなく、ウィジェットそれ自体をアクションとして
扱いたいです。例えば、Wordなどでいえば、アンダーラインスタイルを
変更するのに、ダッシュラインや、ドットラインなどのテキストによる
指示ではなく、____________ や、_ _ _ _ _ _ _ _等の図が、メニューに
現れるようにしたいのですが、どなたか方法をご存知ありませんか?
下に現在トライ中のコードを置きました。メニューは押すと、ごみ粕みたいな
ウィジェットらしきものが出ます。resize
などで元のウィジェットのサイズを
かえようとしても、変わりません。paintEvent内は冗長なコードになっていますが
ご容赦ください。
from PySide import QtGui
from PySide import QtCore
import sys
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.menubar = QtGui.QMenuBar()
self.a_menu = QtGui.QMenu("a-menu")
self.a_menu.resize(500,10)
self.label_A = UnderlineStyleLabel(self.a_menu,QtGui.QTextCharFormat.WaveUnderline)
self.label_A.resize(500,10)
self.widgetAction = QtGui.QWidgetAction(self.a_menu)
# self.widgetAction.setText("DashDotDotLine")
self.widgetAction.createWidget(self.label_A)
self.widgetAction.setDefaultWidget(self.label_A)
self.widgetAction.setEnabled(True)
self.connect(self.widgetAction,QtCore.SIGNAL("triggered()"),self.print_)
self.a_menu.addAction(self.widgetAction)
self.menubar.addMenu(self.a_menu)
self.setMenuBar(self.menubar)
self.show()
def print_(self):
print("A")
class UnderlineStyleLabel(QtGui.QWidget):
def __init__(self,parent=None,underlineStyle=QtGui.QTextCharFormat.NoUnderline):
super(UnderlineStyleLabel,self).__init__(parent=None)
self.setParent(parent)
self.pen_linepath = QtGui.QPainterPath(QtCore.QPointF(0,0))
self.pen_linepath.lineTo(100,0)
self.resize(100,100)
self.underlineStyle = underlineStyle
def paintEvent(self,event):
pen = QtGui.QPen()
if self.underlineStyle == QtGui.QTextCharFormat.NoUnderline:
pen.setStyle(QtCore.Qt.PenStyle.NoPen)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter(self)
painter.setPen(pen)
if not painter.isActive():
painter.begin(self)
painter.drawPath(self.pen_linepath)
painter.end()
elif self.underlineStyle == QtGui.QTextCharFormat.SingleUnderline:
pen.setStyle(QtCore.Qt.PenStyle.SolidLine)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter(self)
painter.setPen(pen)
if not painter.isActive():
painter.begin(self)
painter.drawPath(self.pen_linepath)
painter.end()
elif self.underlineStyle == QtGui.QTextCharFormat.DashUnderline:
pen.setStyle(QtCore.Qt.PenStyle.DashLine)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter(self)
painter.setPen(pen)
if not painter.isActive():
painter.begin(self)
painter.drawPath(self.pen_linepath)
painter.end()
elif self.underlineStyle == QtGui.QTextCharFormat.DotLine:
pen.setStyle(QtCore.Qt.PenStyle.DotLine)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter(self)
painter.setPen(pen)
if not painter.isActive():
painter.begin(self)
painter.drawPath(self.pen_linepath)
painter.end()
elif self.underlineStyle == QtGui.QTextCharFormat.DashDotLine:
pen.setStyle(QtCore.Qt.PenStyle.DashDotLine)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter(self)
painter.setPen(pen)
if not painter.isActive():
painter.begin(self)
painter.drawPath(self.pen_linepath)
painter.end()
elif self.underlineStyle == QtGui.QTextCharFormat.DashDotDotLine:
pen.setStyle(QtCore.Qt.PenStyle.DashDotDotLine)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter(self)
painter.setPen(pen)
if not painter.isActive():
painter.begin(self)
painter.drawPath(self.pen_linepath)
painter.end()
#Wavelineを出したい。
elif self.underlineStyle == QtGui.QTextCharFormat.WaveUnderline:
pen = QtGui.QPen()
pen.setStyle(QtCore.Qt.PenStyle.SolidLine)
pen.setWidth(10)
pen.setColor(QtCore.Qt.black)
painter = QtGui.QPainter()
if not painter.isActive():
painter.begin(self)
painter.setPen(pen)
painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
painter.drawArc(10,10,10,10,480,1920)
painter.drawArc(20,7,10,10,-480,-1920)
painter.drawArc(30,10,10,10,480,1920)
painter.drawArc(40,7,10,10,-480,-1920)
painter.drawArc(50,10,10,10,480,1920)
painter.drawArc(60,7,10,10,-480,-1920)
painter.end()
return QtGui.QWidget.paintEvent(self,event)
def main():
try:
QtGui.QApplication([])
except Exception as e:
print(18,e)
w = Window()
sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
main()