0
votes

I have a little problem with this bit of code:

DatasetWidget::DatasetWidget(QWidget *parent) : QWidget(parent),
ui(new Ui::DatasetWidget)
{
    ui->setupUi(this);

    // cut non-ui related stuff here ...

    // Add widgets
    for(int loop=0;loop<theAmountOfItemsWeAdd;++loop)
    {
        QLabel * ql=new QLabel("Caption");
        QLineEdit * qle=new QLineEdit(this);
        qle->setSizePolicy(QSizePolicy::Policy::Expanding,QSizePolicy::Policy::Fixed);
        ui->formDisp->addRow(ql,qle); // ui->formDisp is of type QFormLayout*
    }

// more non-UI related code

I'm creating a QLineEdit widget and adding it to a QFormLayout, "formDisp". For some weird reason that I can't figure out, the created QLineEdit does not have any frame and setFrame doesn't enable it.

I furthermore append the relevant parts of the .ui file here:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>DatasetWidget</class>
 <widget class="QWidget" name="DatasetWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>723</width>
    <height>591</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>Datensatz</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
      <widget class="QLabel" name="lblCaptionDatensatz">
       <property name="text">
        <string>Datensatz:</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLabel" name="lblDatensatz">
       <property name="font">
        <font>
         <weight>75</weight>
         <bold>true</bold>
         <underline>true</underline>
        </font>
       </property>
       <property name="text">
        <string>C-XXXX-XXX</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLineEdit" name="leDescriptor">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
       <property name="toolTip">
        <string extracomment="ServiceTag"/>
       </property>
       <property name="maxLength">
        <number>7</number>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QPushButton" name="cmdDelete">
       <property name="text">
        <string/>
       </property>
       <property name="icon">
        <iconset resource="hawams.qrc">
         <normaloff>:/icons/res/trash-3x.png</normaloff>:/icons/res/trash-    3x.png</iconset>
       </property>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QFormLayout" name="formDisp"/>
   </item>
   <item>
    <widget class="Line" name="line">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
<connections/>
</ui>

As widgets added in QtCreator look exactly how they are supposed to look, I suppose something about my code must be wrong.

Update: Problem solved: Surrounding QWidget was being used inside a QListView, where it replaced an older, fixed-size version of the widget. In the code that fills the QListView with items, there was a hard-coded size hint which forced Qt to resize the new (bigger) version of the QWidget and "squeeze" it vertically. Conclusion: When you embed a QWidget derivate in buggy old code that resizes it so there isn't enough space anymore to display all items inside the QWidget's layout, things get messy.

2
Please show more of your code because these 4 lines is not enoughHermann
As I don't know which parts of the code are relevant, I appended some more context information.user6468803
Probably a silly question but... are you absolutely sure the QLineEdit doesn't have a frame? On the system I'm sitting at the difference is barely perceptible.G.M.
Do you use stylesheets?thuga
@G.M. I'm absolutely sure - the QLineEdit is on a white background (default background colour on my system for QWidgets that are inside a QListWidget, which is the case with the widget that we're talking about). It definitely doesn't have a frame.user6468803

2 Answers

0
votes

The setFrame(true) is a no-op since the frame is set by default.

Alas, I can't reproduce - the following has both line edits looking identical, save for their size, and their minimum sizes are set to different values, with the second one being larger than first one. A QFormLayout does not enforce a fixed row height!

// https://github.com/KubaO/stackoverflown/tree/master/questions/lineedit-frame-37831979
#include <QtWidgets>
#include <QtUiTools>

const char ui[] = R"EOF(
  <ui version="4.0">
   <class>DatasetWidget</class>
   <widget class="QWidget" name="DatasetWidget">
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="lblCaptionDatensatz">
       <property name="text">
        <string>Datensatz:</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLabel" name="lblDatensatz">
       <property name="text">
        <string>C-XXXX-XXX</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLineEdit" name="leDescriptor">
       <property name="maxLength">
        <number>7</number>
       </property>
      </widget>
     </item>
     <item>
      <layout class="QFormLayout" name="formDisp"></layout>
     </item>
    </layout>
   </widget>
  <connections/>
  </ui>)EOF";

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QBuffer buf;
   buf.setData(ui, sizeof(ui));
   auto w = QUiLoader().load(&buf);
   auto & layout = *w->findChild<QFormLayout*>("formDisp");
   QLineEdit edit1, edit2;
   layout.addRow("Edit1", &edit1);
   layout.addRow("Edit2", &edit2);
   edit1.setMinimumHeight(50);
   edit2.setMinimumHeight(100);
   w->show();
   return app.exec();
}
0
votes

Problem solved. First Widget added to QFormLayout is a QLabel. The row height is then set to the height of the label. All following widgets get stuffed into the "too narrow" rows and if Qt attempted to draw a frame around them, the text line wouldn't fit into them anymore - so there is no frame. Solution: e.g. setMinimumHeight(32) on the first element that is being added to the QFormLayout.

There was also a second mistake. The whole QWidget is the graphical representation of a dataset, and in the main application many instances of this widget are used in a QListView to display the result of a database query. Before the use of the dynamically created QWidget, I had used a static widget with a fixed size. This size was used as sizeHint inside the code that filled the QListView, and I totally forgot about that. After removing the wrong sizeHint in the code of the application's main window, also the setMinimumHeight(someInt) wasn't needed anymore.