Fix QAction::setFont crash on OSX, when font is unknown.

customMenuFont was null, so objects was an empty array, and
NSDictionary throws an exception when being called with arrays
of different sizes.

Task-number: QTBUG-42728
Change-Id: I8cdab449fd8c1d12b65c46dd5617a7f5e3e96c6e
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@theqtcompany.com>
This commit is contained in:
David Faure 2014-11-17 18:35:52 +01:00
parent 6c5d1dbd75
commit eb466b636b
2 changed files with 22 additions and 7 deletions

View File

@ -323,17 +323,22 @@ NSMenuItem *QCocoaMenuItem::sync()
text += QLatin1String(" (") + accel.toString(QKeySequence::NativeText) + QLatin1String(")");
QString finalString = qt_mac_removeMnemonics(text);
bool useAttributedTitle = false;
// Cocoa Font and title
if (m_font.resolve()) {
NSFont *customMenuFont = [NSFont fontWithName:QCFString::toNSString(m_font.family())
size:m_font.pointSize()];
NSArray *keys = [NSArray arrayWithObjects:NSFontAttributeName, nil];
NSArray *objects = [NSArray arrayWithObjects:customMenuFont, nil];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSAttributedString *str = [[[NSAttributedString alloc] initWithString:QCFString::toNSString(finalString)
attributes:attributes] autorelease];
[m_native setAttributedTitle: str];
} else {
if (customMenuFont) {
NSArray *keys = [NSArray arrayWithObjects:NSFontAttributeName, nil];
NSArray *objects = [NSArray arrayWithObjects:customMenuFont, nil];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSAttributedString *str = [[[NSAttributedString alloc] initWithString:QCFString::toNSString(finalString)
attributes:attributes] autorelease];
[m_native setAttributedTitle: str];
useAttributedTitle = true;
}
}
if (!useAttributedTitle) {
[m_native setTitle: QCFString::toNSString(finalString)];
}

View File

@ -61,6 +61,7 @@ private slots:
void setText();
void setIconText_data() { setText_data(); }
void setIconText();
void setUnknownFont();
void actionEvent();
void setStandardKeys();
void alternateShortcuts();
@ -184,6 +185,15 @@ void tst_QAction::setIconText()
QCOMPARE(action.text(), textFromIconText);
}
void tst_QAction::setUnknownFont() // QTBUG-42728
{
QAction action(0);
QFont font("DoesNotExist", 11);
action.setFont(font);
QMenu menu;
menu.addAction(&action); // should not crash
}
void tst_QAction::updateState(QActionEvent *e)
{