Qt World Summit: Register Today!

Displaying a QString in QTextEdit

  • I am new to Qt so please bare with me as this is probably an error on my part or a simple fix I am not aware of.

    I have a SQL database that I am displaying in QTableView where it is editable. I need to be able to display it, when a menu action is called, in a non table format where each record is upto 5 lines with specific info on each line, I dont necessarily need it to be editable in this view so if theres something better to use than QTextEdit please let me know, I have it displaying in the lines I want I just have to add in specifications that I need.

    Where I ran into a problem is when I tried to bold some text as I got it from the model that read in the database and appended it to the QString. I have new line characters appending with the columns that are supposed to be the end of the lines and I also specified a text size of the QTextEdit to increase the text size. When I added the bolding the words I wanted bold showed up as bold but everything now shows up on one line and the text size no longer applies. Here is my code:
    QMainWindow *prevWindow = new QMainWindow;
    QString output;

    for (int row(0); row < model->rowCount(QModelIndex()); ++row){
        QModelIndex indx = model->index(row, 0, QModelIndex());
        if (indx.isValid()){
            if (1 == indx.data().toInt()){
                output.append("Name: \t");
                for (int column(2); column < 13; ++column){
                    QModelIndex idx = model->index(row, column, QModelIndex());
                    if (column == 2)
                        output.append("<b>"+idx.data().toString() + "</b>, ");
                        output.append(idx.data().toString() + " ");
                QModelIndex idx = model->index(row, 1, QModelIndex());
                output.append("\t"+idx.data().toString()+"\n \n");
    prevScreen = new QTextEdit;
    prevWindow->resize(800, 600);


    Line 7 is checking the 1st column which is marked by 0s or 1s for if its an active record, I only want to show the actives. I also am only displaying the 1st line of info from the record which is why I only go through column 13, but I will need to bold something on atleast 3 of the lines.

    does anyone kno how I can do this and keep the new line characters, or some way of making it go to newlines where I have newline characters, and keep the font size I specify?

  • You could globally replace \n with <br> in your string, perhaps.

  • awesome! That worked perfectly, Thanks.

    Do you have any ideas about how to increase the text size?

  • Since the QTextEdit supports rich text, you can use HTML commands (<font> etc.) to adjust text size. Or, you can use the QTextEdit::setFont* methods to set the font globally in the widget.

  • Thanks again mlong! I used <font size = "5"> text </font> and it worked great.

    Another question though. After some sections of the line I need to enter a tab so theres more space and on three of the lines i need the last items lined up together on the right like this

    Name: <tab> Doe, John <tab> Info Service Id
    Service: <tab> Location <tab> info Service Time

    so that Doe and location would be lined up and Service Id and Service Time would line up. And for info just to be tabbed out away from the information before it.
    For the Service Id and Time I tried using something like this <p > text </p> that I saw online and some other similar form but it put them on the next line..

    Any ideas?

    Edit: added the alignment specification I had used because I originally forgot it

Log in to reply