Qpainter DrawText not working on Mac but works fine on Windows when displaying tab "\t"



  • Hello I am trying to port windows application built using QT 4.7 to Mac using Qt 4.8:
    I am having problems with the following code (please note this is only an example to illustrate the problem).
    @
    Void MainWindow::printBox(QPainter *painter, const QString &str1, const QFont &font, const QBrush &brush, const QColor &qColorIn){
    painter->setFont(font);
    int SmallGap = 0;
    int boxWidth = painter->window().width();// - 50;
    int textWidth = boxWidth - 2 * SmallGap;
    int maxHeight = painter->window().height();
    QString myString(“Some text”);
    myString += “\t”; // could use append I get the same results
    myString += “ After tab add more text ”; // could use append I get the same results
    /Output would be = “Some text After tab add more text/
    QRect textRect = painter->boundingRect(
    SmallGap,0, textWidth,maxHeight, Qt::TextExpandTabs, str);

    int boxHeight = textRect.height() ; //+ 2 * SmallGap;
    QPen qpTemp = painter->pen();
    painter->setPen(QPen(qColorIn, 2.0, Qt::SolidLine));   
    painter->setBrush(brush);
    
    painter->drawText(textRect, Qt::TextExpandTabs  , str);
    painter->translate(0,boxHeight);
        painter->setPen(qpTemp);
    

    }
    @
    For some reason on Mac 10.7 OS fails to show any characters after the tab even with Qt::TextExpandTabs included as a flag. This seems to work fine on windows but does not work on Mac.

    Below is a brief description of what I am trying to achieve/ works fine on windows

    ID: 1234567 Age: 23
    Last Name: Smith Weight: 90
    First Name: Joe Height: 170
    As you can see the output should also look like the above with tabs and space according to the names, age, obviously they can be different lengths elc therefore it is required to dynamically generate the correct spacing. The actual code, there is a method that spaces this out correctly but it is not the problem.

    Additional Information:
    Qt Creator 2.6.0
    QT 4.8 for mac
    Mac OS X 10.7.5

    [[Mark up code, Tobias]]



  • Sorry that post was terrible

    OK

    QString myString("Some Text");

    myString += "\t"; // add tab
    myString += "Some more text";

    QRect textRect = painter->boundingRect( 0,0, 3, 5, Qt::TextExpandTabs, myString)

    painter->drawText(textRect, Qt::TextExpandTabs , myString);

    The above code does not generate the tab spaces what I should get is

    OUTPUT SHOULD BE
    Some Text Some more text.

    instead I get

    Some Text.

    The above code seems to work fine on windows but not on mac



  • Notice that QTextOption contains the tabstop-positions. So you may want to try to use the drawText version that takes a QTextOption as an argument, after initializing your tab-stop positions on it.



  • I'm curious, what happens if you draw to a bigger rectangle? It could be getting clipped. Point based font aren't the same on mac or windows, since they are using different dpi's.



  • As steno says, I would bet that indeed this is the issue; the default tab-stops are pixel size based and its likely that on higher resultion screens this means that the text is either moved too far to the right, or even to the next line.

    The code snippet used the boundingRect() to avoid this, at least I assume thats the intent. Unfortunately its used incorrectly. The call uses 0, 0, 3, 5 as a rectangle to put the text in, which is obviously not going to fit even a single character.
    In this case you probably don't want to call the boundingRect() method first, just provide a proper-width rect with a really tall height.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.