how to set geometry of an element in css in qt5

1.6k views Asked by At

I am using qt5-creator designer and iam using css for customizing QPushButtons in a frame i want to set geometry of buttons with css i tried this but this is not working for me

.col-1-row-2 { position: fixed; top: 500 px; left: 500 px; }

i replaced fixed with absolute and relative but nothing changed. how can i set or change default geometry of each element in its css class? and frames geometry?

i use this function to apply the css to the project

void MainWindow::loadCss()
{
int rows = 7, mrows = 4;
int btnWidth = width() / 5,
    btnHeight = height() / (rows + 1);

QString css = "", temp = ".col-%1-row-%2 { top: %3 px; %4: %5 px; }";
for (int i = 1 ; i <= mrows ; ++i)
{
    css += temp.arg(1).arg(i).arg((rows - mrows + i - 1) * btnHeight).arg("left").arg(40);
    css += temp.arg(2).arg(i).arg((rows - mrows + i - 1) * btnHeight).arg("right").arg(40);
}

QFile file(":/css.qss");
file.open(QFile::ReadOnly);
qApp->setStyleSheet(QString::fromLatin1(file.readAll())
                    .replace("${fullWidth}", QString::number(width()))
                    .replace("${fullHeight}", QString::number(height()))
                    .replace("${btnHeight}", QString::number(btnHeight))
                    .replace("${btnWidth}", QString::number(btnWidth))
                    + css
                    );
file.close();
}

qss file

* {
font-size: 40px;
/* qproperty-alignment: AlignCenter; */
background-color: none;
}

QFrame {
position: fixed;
top: 0;
left: 0;
min-width: ${fullWidth} px;
min-height: ${fullHeight} px;
max-width: ${fullWidth} px;
max-height: ${fullHeight} px;
}

QPushButton {
min-width: ${btnWidth} px;
min-height: ${btnHeight} px;
}

#MainWindow {
border-image: url(:/images/sky.jpg) 0 0 0 0 stretch stretch;
border-width: 0px;
}

QFrame css does not working either.

2

There are 2 answers

0
Kuba hasn't forgotten Monica On

It's not supported. Use layouts or manual positioning for that.

Qt implements a self-contained subset of CSS. If something is not documented as supported, it isn't supported. Referring to the list of supported properties: CSS-based geometry management is not supported.

If you insist on styling position using CSS, it would be maybe 10-20 lines to add this functionality to Qt source code.

2
Libor Tomsik On

I'm successfully using qproperty- syntax

QPushButton#Key_0{
   qproperty-geometry: rect(0 0 160 84);
}
QPushButton#Key_1{
   qproperty-geometry: rect(160 0 160 84);
}

As described in Qt documentation