0

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.

H00man
  • 375
  • 1
  • 7
  • 21

2 Answers2

1

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

Libor Tomsik
  • 668
  • 7
  • 24
  • There's a reason why you have to jump through hoops: it's a bad idea. Fixed layouts are by definition broken: suppose the user chose a system font 2x as large for accessibility, or the system element spacing is larger due to accessibility or theme settings, or the language is changed, etc. The problem with answers such as your is while technically it's correct, it gives a shortcut to people who copy-paste stuff without thinking. And the people most likely to produce broken user experiences are those who will use this answer, since they just "want to do it" and don't care why they shouldn't. – Kuba hasn't forgotten Monica Sep 30 '20 at 17:32
  • Well in some industries or in the embedded world are widgets dimensions sometimes given by law (by forcing ISO norms). Here is the pixel precision hunting with layouts nonsense. I will simply transfer the specification into the style. In those cases fixed layouts are 100% fine. – Libor Tomsik Oct 01 '20 at 19:30
0

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313