Important: Please read the Qt Code of Conduct -

Align QLabels and QtableWidgets

  • Hello,
    I am quite new to Qt and have a few questions.
    My main goal is to create an application, that gets data from a device, to be specific it gets 4 numbers.
    I want them to be displayed like this
    length ___
    height ___
    width ___
    weight ___
    so the data would go on the blanks, and also the units behind the data.
    My first approach was to create a lot of Qlabels that display the data on a Qframe and the units, but I can't get them to be in line, when I create them by hand. So first question: can I get labels aligned? But only the labels I choose, not everything, including the buttons?
    My next approach was a QTableWidget, but when I create one in the designer and put it in the middle of the Qframe, I can't connect to it, like tell it to have 3 columns. Only when I create one in my code it does what it should, but it is always locked in the corner of my window.
    How can I get the QtableWidget in the center of my Qframe and fill it with data?

    Thanks a lot for your help or any ideas as to how to implement this application!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From your description it looks like you didn't set any layout manager, did you ?

    You might be interested by the QFormLayout for your QLabel related stuff.

  • No, like I said I am completely new to QT. I only did some tutorial. But I have a layout, that is given and I need to put my Output onto this given QFrame

  • I managed to include a QFormLayout and add 4 Rows in the Designer with QLabels as buddies. But how do I change the text the labels will display? i just don't find any good examples

  • QLabels have, as many other Qt stuff too, a setText(const QString &) function. So you can change the label's displayed text via
    @QLabel::setText("some text");@

    Btw.: You can pass a text to display directly to QLabel's constructor:
    @QLabel* label = new QLabel("some text");@

  • Lifetime Qt Champion

    Look at the Property Editor

  • Ok, I managed to use QLineEdit, because it has the setText Function. I didn't manage however to add a row, that has a QLabel as buddy. That somehow didn't come up in the choices when I click add a row in the Designer.
    But now I have another question: Can I make QLineEdit non editable? So it just displays text and I can't write in it?

  • You realized that QLabel also has this functionallity?
    It's really dirty to abuse QLineEdits just to display text, you have other Qt classes for that.

    But if you adhere to that, the following should do it for you:
    @QLineEdit* edit = new QLineEdit("TextToDisplay");

  • I think I'd rather won't use the Qt Designer. I think it's easier to only code and not mix both. That only confuses me! But thank you for your help!

Log in to reply