I'm recently new to the c++ world. My background is mainly in python and some application specific scripting languages. Anywho, I wanted to get some general feedback and converting my subclass of QLabel written in pyside and convert it to work with a C++ application in Qt Creator. I'm not looking for someone to do the entire project for me. I just need some guidance on how to add/setup a custom control in my Qt project.
You'll notice in my subclass I've simply just overwritten the paint event of the label in order to create the dotted pattern to fill the empty space to the right of the label as seen here:
Code for my custom label in Pyside:
class QLabelHeader(QtWidgets.QLabel):
def __init__(self, parent=None, **kwargs):
super(QLabelHeader, self).__init__(parent)
# events
def paintEvent(self, event):
# calculate font width
metrics = QtGui.QFontMetrics(self.font())
text_width = metrics.boundingRect(self.text()).width()
# calculate dimensions
y = int(self.height() * 0.5 - 2)
x = text_width + 4
width = self.width() - x
# create pattern
px = QtGui.QPixmap(4,4)
px.fill(QtCore.Qt.transparent)
pattern_painter = QtGui.QPainter(px)
pattern_painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))
pattern_painter.setBrush(QtGui.QBrush(QtGui.QColor(200,200,200), QtCore.Qt.SolidPattern))
pattern_painter.drawRect(0,0,1,1)
pattern_painter.drawRect(2,2,1,1)
pattern_painter.end()
# draw tiled pixmap
painter = QtGui.QPainter(self)
painter.drawTiledPixmap(x,y,width,5,px)
painter.end()
super(QLabelHeader, self).paintEvent(event)
Question:
I've seen other sample projects online that include custom controls with a folder structure like seen here, which is a subfolder within a larger project. How do they create this folder structure and it's set of files?
As an added bonus if anyone feels like showing me a preview/psuedo code of what my h and cpp file would look like for overriding the QLabel's paint event like my pyside code.