Search code examples
c++qtdesigner

Programatically make a QToolButton identical to one from Qt Designer


I need to make a QToolButton that looks exactly like the default QToolButton you get when you drag one into your form in Qt Designer. Simply creating a QToolButton like this:

QToolButton *toolButton = new QToolButton(this)

does not work. You get a button, but it has no background (although it does display text). I tried creating a QToolButton in a Ui file to copy from, but you can't copy one, it's just not possible.

Any ideas on how to perform something this simple?!


Solution

  • It works for me, but you do have to set the geometry/text on it.

    you can look in the file generated by UIC on your ui file with an example QToolButton to see what you are missing.

    Make sure that the toolbutton has the correct parent - this could result in drawing strangeness if it is wrong.

    e.g:

    QToolButton* tb = new QToolButton(ui.centralWidget);
    tb->setGeometry(QRect(10, 40, 25, 19));
    tb->setText("...");