Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter problem when trying to draw a push button on a toolbar.
Forum Updated to NodeBB v4.3 + New Features

QPainter problem when trying to draw a push button on a toolbar.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bigLuke09
    wrote on last edited by
    #1

    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?

    @MyToolBarActions::MyToolBarActions(QObject* p)
    : QActionGroup(p)
    {
    QIcon icon = qApp->style()->standardIcon(QStyle::SP_MessageBoxCritical);
    QStyleOptionButton opt;
    opt.state = QStyle::State_Active | QStyle::State_Enabled;
    opt.rect = QRect(50, 25, 100, 50);
    QPainter painter;
    painter.drawText(55, 30, 80, 30, Qt::AlignCenter, "MyAction");
    painter.beginNativePainting();
    qApp->style()->drawControl(QStyle::CE_PushButton,&opt,&painter);
    painter.endNativePainting();
    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()));
    }@

    Thanks much.

    1 Reply Last reply
    1
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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...

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        steno
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bigLuke09
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            steno
            wrote on last edited by
            #5

            Look into QWidgetAction. It allows you to put widgets into toolbars.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              You can use "addWidget()":/doc/qt-4.8/qtoolbar.html#addWidget on the toolbar to add your QComboBox.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bigLuke09
                wrote on last edited by
                #7

                Thank you. You have been very helpful.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved