Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use Scroll Area in Qt to print a QString array



  • Hi everyone,

    I am trying to understand how to use "Scroll Area" in "Containers" of Qt when designing GUIs. In my case, I have a display function which displays the data from a hash table of objects into the scroll area. Each element of the object stored at the specified index of the array is of type QString and will be printed onto the area separated by a comma and would also need to make it so that once the last element of the object (phone) is returned, the next object is printed onto the next line. I have taken a look at the documentation but haven't found anything of use for what I'm looking. I am not looking to create a parent or child since I don't understand that concept. Ideally, I would be looking for something of the sorts of ui->scrollAreaContents-> since it is simple to use. I have posted my code for the function below. Thanks in advance.

    void Database::display(){
    for(int i = 0: i < TABLESIZE; i++)
    {
    table[i]->getName();
    table[i]->getJob();
    table[i]->getVehicle();
    table[i]->getAddress();
    table[i]->getCountry();
    table[I]->getPhone();

    }
    }



  • @jefazo92
    Hi there. In the politest way, I'm afraid what you write/ask does not make much sense! You need to do some reading/playing to get the hang of Qt widgets.

    A QScrollArea is just a container, for other widgets, which happens to have a fixed size and allows scrolling via scrollbars if its content exceeds its size.

    Basically, what you probably mean you want to do for your example is create widgets --- like QLabel, QLineEdit or QTextEdit --- for each of the texts of your data fields, and then add those widgets onto a QScrollArea (probably inside some kind of QLayouts).

    Having said that, you might find that using, say, a QTableView or QTableWidget whose model is your data, is what you really want. That will lay out your data in rows & columns. If that table itself can get too high or wide to fit, you would enclose the table inside a QScrollArea and scroll the table as a whole up/down/right/left.



  • Hi @JonB JonB, just saw your other message. Don't know what happened there. I only posted once.



  • @jefazo92
    OK. Like I said, forget about scrolling for now. You need to get the hang of just adding what Qt calls "widgets" to display your data.

    Since you have a loop for rows and then various columns in each row, a QTableView/QTableWidget would be the first thought for presentation. Rather than talking about "commas" & "lines".


Log in to reply