| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 3 | |
Hi Everyone, Now I'm trying the QT multi-thread support. It is very simple and straight- forward to a rookie UI designer like me. But I found concern about the thread-safe of memory allocation. Because I need to port my source code to many different platforms, such as 32-bit machines (SOLARIS, Linux), and 64-bit machines (LinuxAMD64, HP11, IBM5 ...). QT use malloc to acquire memory from OS. I'm sure QT is thread safe. But I'm not sure malloc is thread safe on all these platforms. It is a compiler- dependent issue. Could anybody help me out ?? Regards, Pagan -- [ signature omitted ]
Hi Pagen, If memory serves me: I believe the 'rule' in Qt is that you must delete a Qt object on the same thread on which it was created. If you violate this, you get a friendly exception at runtime. Mark Pagan Chou wrote: > Hi Everyone, > Now I'm trying the QT multi-thread support. It is very simple and straight- > forward to a rookie UI designer like me. But I found concern about the > thread-safe of memory allocation. > > Because I need to port my source code to many different platforms, such > as 32-bit machines (SOLARIS, Linux), and 64-bit machines (LinuxAMD64, > HP11, IBM5 ...). > > QT use malloc to acquire memory from OS. I'm sure QT is thread safe. > But I'm not sure malloc is thread safe on all these platforms. It is a > compiler- > dependent issue. > > Could anybody help me out ?? > > Regards, > Pagan > > > -- > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body. > List archive and information: http://lists.trolltech.com/qt-interest/ > > > > > -- [ signature omitted ]
Hello all,
We're implementing a message viewer and we've implemented a custom
delegate for a QStandardItemView that enables us to switch from hex to
decimal. The problem is that the automatic sizing of columns is now
broken. So we get something that looks like table1.jpg when we really
should be getting something that looks like table2.jpg (both attached)
when we do an resizeColumnsToContents(). The two columns that are cut
off (time and data) are the two columns we nurgle in the delegate.
I've never actually done a sizeHint before, so I don't really know what
I'm supposed to do, or whether this is even the RIGHT thing to do. The
text mentions using QHeaderView::resizeSection, but then I'll be trying
to work out the size of the object from outside of the thing which is
drawing it, which doesn't seem OO to me.
I've played with the sizeHint function, and it definatley affects it,
but if someone can point me in the right direction as to how I can work
out the size of the text I would much appreciate it.
Here is my code:
//! Paints the item at \p index of the model associated with the
QModelIndex
//! in either hex or decimal display format.
void LogTableDisplayDelegate::paint(QPainter* painter, const
QStyleOptionViewItem& option,
const QModelIndex& index, const int format =
SetAsHex) const
{
QString elementText = "";
Uint16 maxNumCharacters = 0; //used to fill 0 values if it doesn't
fill that many characters
if (format == SetAsDecimal)
{
maxNumCharacters = 3;
}
else if (format == SetAsHex)
{
maxNumCharacters = 2;
}
if (index.column() == SelectHeaderDialog::dataColumn) //display the
message data column
{
QByteArray byteArray = index.model()->data(index,
Qt::DisplayRole).toByteArray();
Uint32 payloadSize = byteArray.length();
const char* data = byteArray.constData();
for (Uint32 counter = 0; counter < payloadSize; counter++)
{
QString tempStr =
QString("%1").arg(static_cast<Uint8>(data[counter]),
maxNumCharacters,
format, QLatin1Char('0'));
tempStr = tempStr.toUpper();
elementText.append(tempStr);
elementText.append(" ");
}
}
else if (index.column() == SelectHeaderDialog::timeColumn) //display
the time column
{
//these convert the miliseconds into seconds/minutes/hours
static const double secondsMultiplier = 1.0 / 1000.0;
static const double minutesMultiplier = 1.0 / 60000.0;
static const double hoursMultiplier = 1.0 / 3600000.0;
Uint32 mSeconds = index.model()->data(index,
Qt::DisplayRole).toInt();
Uint32 seconds = mSeconds * secondsMultiplier;//same as dividing
by 1000 but quicker
Uint32 minutes = mSeconds * minutesMultiplier;//same as dividing
by 60000;
Uint32 hours = mSeconds * hoursMultiplier; //same as dividing
by 3600000;
mSeconds = mSeconds % 1000;
elementText = QString("%1:%2:%3:%4")
.arg(hours, 2, 10, QLatin1Char('0'))
.arg(minutes, 2, 10, QLatin1Char('0'))
.arg(seconds, 2, 10, QLatin1Char('0'))
.arg(mSeconds, 3, 10, QLatin1Char('0'));
}
else
{
const Uint32 value = index.model()->data(index,
Qt::DisplayRole).toInt();
elementText = QString("%1").arg(value, maxNumCharacters,
format, QLatin1Char('0'));
elementText = elementText.toUpper();
}
//this code was copied from qitemdelegate.cpp's paint function
//it was slightly modified by setting the appropriate pen color
// draw the background color
if (option.showDecorationSelected && (option.state &
QStyle::State_Selected))
{
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal :
QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state &
QStyle::State_Active))
{
cg = QPalette::Inactive;
}
painter->fillRect(option.rect, option.palette.brush(cg,
QPalette::Highlight));
painter->setPen(Qt::white); //added this to copied code
}
else
{
QVariant value = index.data(Qt::BackgroundColorRole);
if (value.isValid() && qvariant_cast<QColor>(value).isValid())
{
painter->fillRect(option.rect,
qvariant_cast<QColor>(value));
}
painter->setPen(Qt::black); //added this to copied code
}
//end of copied code
//draw the text
painter->setFont(QFont("Courier New", 8));
painter->drawText(option.rect, Qt::AlignCenter | Qt::TextSingleLine,
elementText);
}
QSize LogTableDisplayDelegate::sizeHint(const QStyleOptionViewItem&
option, const QModelIndex& index) const
{
if (index.column() == SelectHeaderDialog::dataColumn)
{
return QSize(900, 20);
}
else
{
return QItemDelegate::sizeHint(option, index);
}
}
Attachment:
Attachment:
table1.JPG Attachment:
table2.JPG
Description: Binary data
Description: Binary data
Message 4 in thread
For those whome care, it was a problem caused by the paint function
rendering the underlying data in a way which the sizeHint did not see.
That is, the underlying data was a QByteArray, and the paint function
formatted it to a nice Qstring with spaces and printed it, but the
sizeHint only returned the size of the QByteArray. So we just refactored
and ended up with a sizehint like this:
QSize LogTableDisplayDelegate::sizeHint(const QStyleOptionViewItem&
option, const QModelIndex& index) const
{
QSize result;
if (index.column() == SelectHeaderDialog::dataColumn)
{
QString dataField = FormatDataAsString(index.model()->data(index,
Qt::DisplayRole).toByteArray());
QFontMetrics fm(font_);
result = fm.size(Qt::TextSingleLine, dataField);
}
else if (index.column() == SelectHeaderDialog::timeColumn)
{
QString timeField = FormatTimeAsString(index.model()->data(index,
Qt::DisplayRole).toUInt());
QFontMetrics fm(font_);
result = fm.size(Qt::TextSingleLine, timeField);
}
else
{
result = QItemDelegate::sizeHint(option, index);
}
return result;
}
Where the FormatDataAsString and FormatTimeAsString are functions called
from the paint routine as well.
--
[ signature omitted ]
Message 5 in thread
A quick google search found this:
http://www.gnu.org/software/libc/FAQ.html#s-3.19
But of course it's not definitive. I think you'll be safe on any
platform using glibc 2.
I guess the best test is to write a program which has two threads which
constantly try to allocate memory and see if you get heap corruption.
My gut says that no modern malloc implementation will be thread unsafe
though.... (at least on the platforms you can run Qt on ;) )
Cheers
Ben
> -----Original Message-----
> From: Pagan Chou [mailto:pagan_chou@xxxxxxxxxxxxxx]
> Sent: Monday, December 11, 2006 5:47 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: QT multi-thread question
>
> Hi Everyone,
> Now I'm trying the QT multi-thread support. It is very simple
> and straight- forward to a rookie UI designer like me. But I
> found concern about the thread-safe of memory allocation.
>
> Because I need to port my source code to many different
> platforms, such as 32-bit machines (SOLARIS, Linux), and
> 64-bit machines (LinuxAMD64, HP11, IBM5 ...).
>
> QT use malloc to acquire memory from OS. I'm sure QT is thread safe.
> But I'm not sure malloc is thread safe on all these platforms. It is a
> compiler-
> dependent issue.
>
> Could anybody help me out ??
>
> Regards,
> Pagan
>
>
> --
> To unsubscribe - send a mail to
> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the
> subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]