Home > front end >  How to specify style sheets for my classes in QT
How to specify style sheets for my classes in QT

Time:03-14

I have a class that inherits from qframe, the "darksheetstyle" qss file is:

    /* (dot) .QFrame  fix #141, #126, #123 */
.QFrame {
  border-radius: 4px;
  border: 1px solid #455364;
  /* No frame */
  /* HLine */
  /* HLine */
}

.QFrame[frameShape="0"] {
  border-radius: 4px;
  border: 1px transparent #455364;
}

.QFrame[frameShape="4"] {
  max-height: 2px;
  border: none;
  background-color: #455364;
}

.QFrame[frameShape="5"] {
  max-width: 2px;
  border: none;
  background-color: #455364;
}

the QFrame has no border, so I wrote my class qss in the same qss file:

BorderFrame {
  border-radius: 4px;
  border: 2px solid #455364;
  /* No frame */
  /* HLine */
  /* HLine */
}

the cpp code is:

class ULTRA_PLOT_EXPORT BorderedFrame : public QFrame
    {
        Q_OBJECT
    public:
        explicit BorderedFrame(QWidget* parent) : QFrame(parent)
        {
            setParent(parent);
        }
        ~BorderedFrame() override = default;
        BorderedFrame(const BorderedFrame&) = delete;
        BorderedFrame& operator=(const BorderedFrame&) = delete;
        BorderedFrame(BorderedFrame&&) = delete;
        BorderedFrame& operator=(BorderedFrame&&) = delete;
        void paintEvent(QPaintEvent* event) override
        {
            QStyleOption opt;
            opt.initFrom(this);
            QPainter p(this);
            style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);//绘制样式
        }
    };

then, My own QFrame class still has no borders, how to fixed it ?

CodePudding user response:

This works as expected for me:

#include <QtWidgets>

class BorderedFrame : public QFrame
{
  Q_OBJECT
public:
  using QFrame::QFrame;
};

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  app.setStyleSheet("BorderedFrame { border-radius: 4px; border: 2px solid #ff0000; }");
  BorderedFrame w;
  w.show();

  return app.exec();
}

#include "main.moc"
  • Related