QPainter problem when trying to draw a push button on a toolbar.
Hello, I'm new to Qt and using it for my thesis. This toolbar is utilized by a program called Paraview and I want to add functionality to it. When I try to add this push button below, it compiles fine, but I'm getting an error saying the painter isn't active. How do I enable it?
QIcon icon = qApp->style()->standardIcon(QStyle::SP_MessageBoxCritical);
opt.state = QStyle::State_Active | QStyle::State_Enabled;
opt.rect = QRect(50, 25, 100, 50);
painter.drawText(55, 30, 80, 30, Qt::AlignCenter, "MyAction");
QAction* a = this->addAction(new QAction(icon, "MyAction", this));
//QAction* a2 = this->addAction(new QAction(icon2, "MyAction2",this));
QObject::connect(a, SIGNAL(triggered(bool)), this, SLOT(onAction()));
You can only use a painter during a widget's paintEvent(), not just anywhere to decorate an item.
Why are you trying to draw a push button? Why not just create a QToolButton and add it to the tool bar? Or better yet, just create your QAction and add it to the toolbar...
You can't paint onto a widget outside of the paintEvent. I would suggest reading the documentation of QPainter, it will clear up a lot for you.
Thanks, the documentation cleared that up and that part is pointless. Actually, I'm just trying to draw the button as an intermediate step. I've already created QActions with them. Ultimately, I want to add a QComboBox to the Toolbar with a bunch of items, but everything I've tried is failing, so I started simpler. Any suggestions on how to implement the QComboBox?
Look into QWidgetAction. It allows you to put widgets into toolbars.
You can use "addWidget()":/doc/qt-4.8/qtoolbar.html#addWidget on the toolbar to add your QComboBox.
Thank you. You have been very helpful.