QtBase: example code style

Change-Id: I0685f7e2c7c698a4bf288b8b1ae909167932a3fb
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
David Schulz 2012-11-23 14:27:50 +01:00 committed by The Qt Project
parent d5ec06be63
commit 4ac521354d
32 changed files with 83 additions and 93 deletions

View File

@ -38,10 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include "chat.h" #include <QApplication>
#include <QtWidgets/QApplication> #include <QMessageBox>
#include <QtWidgets/QMessageBox>
#include "chat.h"
#include "chat_adaptor.h" #include "chat_adaptor.h"
#include "chat_interface.h" #include "chat_interface.h"

View File

@ -41,8 +41,8 @@
#ifndef CHAT_H #ifndef CHAT_H
#define CHAT_H #define CHAT_H
#include <QtCore/QStringList> #include <QStringList>
#include <QtDBus/QtDBus>
#include "ui_chatmainwindow.h" #include "ui_chatmainwindow.h"
#include "ui_chatsetnickname.h" #include "ui_chatsetnickname.h"
@ -78,4 +78,4 @@ public:
NicknameDialog(QWidget *parent = 0); NicknameDialog(QWidget *parent = 0);
}; };
#endif #endif // CHAT_H

View File

@ -38,10 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QApplication>
#include "mandelbrotwidget.h" #include "mandelbrotwidget.h"
#include <QApplication>
//! [0] //! [0]
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {

View File

@ -38,7 +38,8 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets> #include <QPainter>
#include <QKeyEvent>
#include <math.h> #include <math.h>
@ -65,8 +66,7 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent)
curScale = DefaultScale; curScale = DefaultScale;
qRegisterMetaType<QImage>("QImage"); qRegisterMetaType<QImage>("QImage");
connect(&thread, SIGNAL(renderedImage(QImage,double)), connect(&thread, SIGNAL(renderedImage(QImage,double)), this, SLOT(updatePixmap(QImage,double)));
this, SLOT(updatePixmap(QImage,double)));
setWindowTitle(tr("Mandelbrot")); setWindowTitle(tr("Mandelbrot"));
#ifndef QT_NO_CURSOR #ifndef QT_NO_CURSOR
@ -85,8 +85,7 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
if (pixmap.isNull()) { if (pixmap.isNull()) {
painter.setPen(Qt::white); painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, painter.drawText(rect(), Qt::AlignCenter, tr("Rendering initial image, please wait..."));
tr("Rendering initial image, please wait..."));
//! [2] //! [3] //! [2] //! [3]
return; return;
//! [3] //! [4] //! [3] //! [4]
@ -122,11 +121,9 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
painter.setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0, 0, 0, 127)); painter.setBrush(QColor(0, 0, 0, 127));
painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10, painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10, metrics.lineSpacing() + 5);
metrics.lineSpacing() + 5);
painter.setPen(Qt::white); painter.setPen(Qt::white);
painter.drawText((width() - textWidth) / 2, painter.drawText((width() - textWidth) / 2, metrics.leading() + metrics.ascent(), text);
metrics.leading() + metrics.ascent(), text);
} }
//! [9] //! [9]

View File

@ -81,4 +81,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // MANDELBROTWIDGET_H

View File

@ -38,12 +38,12 @@
** **
****************************************************************************/ ****************************************************************************/
#include "renderthread.h"
#include <QtWidgets> #include <QtWidgets>
#include <math.h> #include <math.h>
#include "renderthread.h"
//! [0] //! [0]
RenderThread::RenderThread(QObject *parent) RenderThread::RenderThread(QObject *parent)
: QThread(parent) : QThread(parent)

View File

@ -59,8 +59,7 @@ public:
RenderThread(QObject *parent = 0); RenderThread(QObject *parent = 0);
~RenderThread(); ~RenderThread();
void render(double centerX, double centerY, double scaleFactor, void render(double centerX, double centerY, double scaleFactor, QSize resultSize);
QSize resultSize);
signals: signals:
void renderedImage(const QImage &image, double scaleFactor); void renderedImage(const QImage &image, double scaleFactor);
@ -85,4 +84,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // RENDERTHREAD_H

View File

@ -46,8 +46,7 @@
Screenshot::Screenshot() Screenshot::Screenshot()
{ {
screenshotLabel = new QLabel; screenshotLabel = new QLabel;
screenshotLabel->setSizePolicy(QSizePolicy::Expanding, screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QSizePolicy::Expanding);
screenshotLabel->setAlignment(Qt::AlignCenter); screenshotLabel->setAlignment(Qt::AlignCenter);
screenshotLabel->setMinimumSize(240, 160); screenshotLabel->setMinimumSize(240, 160);
@ -73,8 +72,7 @@ void Screenshot::resizeEvent(QResizeEvent * /* event */)
{ {
QSize scaledSize = originalPixmap.size(); QSize scaledSize = originalPixmap.size();
scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio); scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
if (!screenshotLabel->pixmap() if (!screenshotLabel->pixmap() || scaledSize != screenshotLabel->pixmap()->size())
|| scaledSize != screenshotLabel->pixmap()->size())
updateScreenshotLabel(); updateScreenshotLabel();
} }
//! [1] //! [1]
@ -96,11 +94,10 @@ void Screenshot::saveScreenshot()
QString format = "png"; QString format = "png";
QString initialPath = QDir::currentPath() + tr("/untitled.") + format; QString initialPath = QDir::currentPath() + tr("/untitled.") + format;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), initialPath,
initialPath, tr("%1 Files (*.%2);;All Files (*)")
tr("%1 Files (*.%2);;All Files (*)") .arg(format.toUpper())
.arg(format.toUpper()) .arg(format));
.arg(format));
if (!fileName.isEmpty()) if (!fileName.isEmpty())
originalPixmap.save(fileName, format.toLatin1().constData()); originalPixmap.save(fileName, format.toLatin1().constData());
} }
@ -132,9 +129,9 @@ void Screenshot::updateCheckBox()
if (delaySpinBox->value() == 0) { if (delaySpinBox->value() == 0) {
hideThisWindowCheckBox->setDisabled(true); hideThisWindowCheckBox->setDisabled(true);
hideThisWindowCheckBox->setChecked(false); hideThisWindowCheckBox->setChecked(false);
} } else {
else
hideThisWindowCheckBox->setDisabled(false); hideThisWindowCheckBox->setDisabled(false);
}
} }
//! [6] //! [6]
@ -163,12 +160,8 @@ void Screenshot::createOptionsGroupBox()
//! [8] //! [8]
void Screenshot::createButtonsLayout() void Screenshot::createButtonsLayout()
{ {
newScreenshotButton = createButton(tr("New Screenshot"), newScreenshotButton = createButton(tr("New Screenshot"), this, SLOT(newScreenshot()));
this, SLOT(newScreenshot())); saveScreenshotButton = createButton(tr("Save Screenshot"), this, SLOT(saveScreenshot()));
saveScreenshotButton = createButton(tr("Save Screenshot"),
this, SLOT(saveScreenshot()));
quitScreenshotButton = createButton(tr("Quit"), this, SLOT(close())); quitScreenshotButton = createButton(tr("Quit"), this, SLOT(close()));
buttonsLayout = new QHBoxLayout; buttonsLayout = new QHBoxLayout;

View File

@ -75,8 +75,7 @@ private slots:
private: private:
void createOptionsGroupBox(); void createOptionsGroupBox();
void createButtonsLayout(); void createButtonsLayout();
QPushButton *createButton(const QString &text, QWidget *receiver, QPushButton *createButton(const QString &text, QWidget *receiver, const char *member);
const char *member);
void updateScreenshotLabel(); void updateScreenshotLabel();
QPixmap originalPixmap; QPixmap originalPixmap;
@ -96,4 +95,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // SCREENSHOT_H

View File

@ -66,10 +66,8 @@ Window::Window()
createTrayIcon(); createTrayIcon();
connect(showMessageButton, SIGNAL(clicked()), this, SLOT(showMessage())); connect(showMessageButton, SIGNAL(clicked()), this, SLOT(showMessage()));
connect(showIconCheckBox, SIGNAL(toggled(bool)), connect(showIconCheckBox, SIGNAL(toggled(bool)), trayIcon, SLOT(setVisible(bool)));
trayIcon, SLOT(setVisible(bool))); connect(iconComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setIcon(int)));
connect(iconComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(setIcon(int)));
connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked())); connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
@ -129,8 +127,7 @@ void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
switch (reason) { switch (reason) {
case QSystemTrayIcon::Trigger: case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick: case QSystemTrayIcon::DoubleClick:
iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count());
% iconComboBox->count());
break; break;
case QSystemTrayIcon::MiddleClick: case QSystemTrayIcon::MiddleClick:
showMessage(); showMessage();

View File

@ -159,9 +159,9 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
child->setPixmap(tempPixmap); child->setPixmap(tempPixmap);
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
child->close(); child->close();
else { } else {
child->show(); child->show();
child->setPixmap(pixmap); child->setPixmap(pixmap);
} }

View File

@ -52,7 +52,7 @@ QT_END_NAMESPACE
class DragWidget : public QFrame class DragWidget : public QFrame
{ {
public: public:
DragWidget(QWidget *parent=0); DragWidget(QWidget *parent = 0);
protected: protected:
void dragEnterEvent(QDragEnterEvent *event); void dragEnterEvent(QDragEnterEvent *event);
@ -62,4 +62,4 @@ protected:
}; };
//! [0] //! [0]
#endif #endif // DRAGWIDGET_H

View File

@ -40,6 +40,7 @@
#include <QApplication> #include <QApplication>
#include <QHBoxLayout> #include <QHBoxLayout>
#include "dragwidget.h" #include "dragwidget.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -38,8 +38,6 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "draglabel.h" #include "draglabel.h"
DragLabel::DragLabel(const QString &text, QWidget *parent) DragLabel::DragLabel(const QString &text, QWidget *parent)

View File

@ -55,4 +55,4 @@ public:
DragLabel(const QString &text, QWidget *parent); DragLabel(const QString &text, QWidget *parent);
}; };
#endif #endif // DRAGLABEL_H

View File

@ -147,8 +147,7 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
QMimeData *mimeData = new QMimeData; QMimeData *mimeData = new QMimeData;
mimeData->setText(child->text()); mimeData->setText(child->text());
mimeData->setData("application/x-hotspot", mimeData->setData("application/x-hotspot",
QByteArray::number(hotSpot.x()) QByteArray::number(hotSpot.x()) + " " + QByteArray::number(hotSpot.y()));
+ " " + QByteArray::number(hotSpot.y()));
QPixmap pixmap(child->size()); QPixmap pixmap(child->size());
child->render(&pixmap); child->render(&pixmap);

View File

@ -59,4 +59,4 @@ protected:
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
}; };
#endif #endif // DRAGWIDGET_H

View File

@ -39,6 +39,7 @@
****************************************************************************/ ****************************************************************************/
#include <QApplication> #include <QApplication>
#include "dragwidget.h" #include "dragwidget.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -38,10 +38,11 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "droparea.h" #include "droparea.h"
#include <QDragEnterEvent>
#include <QMimeData>
//! [DropArea constructor] //! [DropArea constructor]
DropArea::DropArea(QWidget *parent) DropArea::DropArea(QWidget *parent)
: QLabel(parent) : QLabel(parent)

View File

@ -74,4 +74,4 @@ private:
}; };
//! [DropArea header part2] //! [DropArea header part2]
#endif #endif // DROPAREA_H

View File

@ -74,4 +74,4 @@ private:
}; };
//! [DropSiteWindow header] //! [DropSiteWindow header]
#endif #endif // DROPSITEWINDOW_H

View File

@ -38,10 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "draglabel.h" #include "draglabel.h"
#include <QtWidgets>
//! [0] //! [0]
DragLabel::DragLabel(const QString &text, QWidget *parent) DragLabel::DragLabel(const QString &text, QWidget *parent)
: QLabel(parent) : QLabel(parent)
@ -49,8 +49,7 @@ DragLabel::DragLabel(const QString &text, QWidget *parent)
QFontMetrics metric(font()); QFontMetrics metric(font());
QSize size = metric.size(Qt::TextSingleLine, text); QSize size = metric.size(Qt::TextSingleLine, text);
QImage image(size.width() + 12, size.height() + 12, QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied);
QImage::Format_ARGB32_Premultiplied);
image.fill(qRgba(0, 0, 0, 0)); image.fill(qRgba(0, 0, 0, 0));
QFont font; QFont font;

View File

@ -61,4 +61,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // DRAGLABEL_H

View File

@ -38,11 +38,11 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "draglabel.h" #include "draglabel.h"
#include "dragwidget.h" #include "dragwidget.h"
#include <QtWidgets>
//! [0] //! [0]
DragWidget::DragWidget(QWidget *parent) DragWidget::DragWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent)

View File

@ -62,4 +62,4 @@ protected:
}; };
//! [0] //! [0]
#endif #endif // DRAGWIDGET_H

View File

@ -39,6 +39,7 @@
****************************************************************************/ ****************************************************************************/
#include <QApplication> #include <QApplication>
#include "dragwidget.h" #include "dragwidget.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -38,13 +38,13 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include <stdlib.h>
#include "mainwindow.h" #include "mainwindow.h"
#include "pieceslist.h" #include "pieceslist.h"
#include "puzzlewidget.h" #include "puzzlewidget.h"
#include <QtWidgets>
#include <stdlib.h>
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
{ {
@ -59,9 +59,10 @@ void MainWindow::openImage(const QString &path)
{ {
QString fileName = path; QString fileName = path;
if (fileName.isNull()) if (fileName.isNull()) {
fileName = QFileDialog::getOpenFileName(this, fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)"); tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)");
}
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
QPixmap newImage; QPixmap newImage;
@ -79,9 +80,9 @@ void MainWindow::openImage(const QString &path)
void MainWindow::setCompleted() void MainWindow::setCompleted()
{ {
QMessageBox::information(this, tr("Puzzle Completed"), QMessageBox::information(this, tr("Puzzle Completed"),
tr("Congratulations! You have completed the puzzle!\n" tr("Congratulations! You have completed the puzzle!\n"
"Click OK to start again."), "Click OK to start again."),
QMessageBox::Ok); QMessageBox::Ok);
setupPuzzle(); setupPuzzle();
} }

View File

@ -73,4 +73,4 @@ private:
PuzzleWidget *puzzleWidget; PuzzleWidget *puzzleWidget;
}; };
#endif #endif // MAINWINDOW_H

View File

@ -38,10 +38,12 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "pieceslist.h" #include "pieceslist.h"
#include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
PiecesList::PiecesList(int pieceSize, QWidget *parent) PiecesList::PiecesList(int pieceSize, QWidget *parent)
: QListWidget(parent), m_PieceSize(pieceSize) : QListWidget(parent), m_PieceSize(pieceSize)
{ {
@ -66,8 +68,9 @@ void PiecesList::dragMoveEvent(QDragMoveEvent *event)
if (event->mimeData()->hasFormat("image/x-puzzle-piece")) { if (event->mimeData()->hasFormat("image/x-puzzle-piece")) {
event->setDropAction(Qt::MoveAction); event->setDropAction(Qt::MoveAction);
event->accept(); event->accept();
} else } else {
event->ignore(); event->ignore();
}
} }
void PiecesList::dropEvent(QDropEvent *event) void PiecesList::dropEvent(QDropEvent *event)
@ -83,8 +86,9 @@ void PiecesList::dropEvent(QDropEvent *event)
event->setDropAction(Qt::MoveAction); event->setDropAction(Qt::MoveAction);
event->accept(); event->accept();
} else } else {
event->ignore(); event->ignore();
}
} }
void PiecesList::addPiece(QPixmap pixmap, QPoint location) void PiecesList::addPiece(QPixmap pixmap, QPoint location)
@ -93,8 +97,7 @@ void PiecesList::addPiece(QPixmap pixmap, QPoint location)
pieceItem->setIcon(QIcon(pixmap)); pieceItem->setIcon(QIcon(pixmap));
pieceItem->setData(Qt::UserRole, QVariant(pixmap)); pieceItem->setData(Qt::UserRole, QVariant(pixmap));
pieceItem->setData(Qt::UserRole+1, location); pieceItem->setData(Qt::UserRole+1, location);
pieceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable pieceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
| Qt::ItemIsDragEnabled);
} }
void PiecesList::startDrag(Qt::DropActions /*supportedActions*/) void PiecesList::startDrag(Qt::DropActions /*supportedActions*/)

View File

@ -60,4 +60,4 @@ protected:
int m_PieceSize; int m_PieceSize;
}; };
#endif #endif // PIECESLIST_H

View File

@ -38,10 +38,13 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "puzzlewidget.h" #include "puzzlewidget.h"
#include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QPainter>
PuzzleWidget::PuzzleWidget(int imageSize, QWidget *parent) PuzzleWidget::PuzzleWidget(int imageSize, QWidget *parent)
: QWidget(parent), m_ImageSize(imageSize) : QWidget(parent), m_ImageSize(imageSize)
{ {
@ -130,9 +133,8 @@ void PuzzleWidget::dropEvent(QDropEvent *event)
int PuzzleWidget::findPiece(const QRect &pieceRect) const int PuzzleWidget::findPiece(const QRect &pieceRect) const
{ {
for (int i = 0; i < pieceRects.size(); ++i) { for (int i = 0; i < pieceRects.size(); ++i) {
if (pieceRect == pieceRects[i]) { if (pieceRect == pieceRects[i])
return i; return i;
}
} }
return -1; return -1;
} }
@ -192,9 +194,8 @@ void PuzzleWidget::paintEvent(QPaintEvent *event)
painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1)); painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
} }
for (int i = 0; i < pieceRects.size(); ++i) { for (int i = 0; i < pieceRects.size(); ++i)
painter.drawPixmap(pieceRects[i], piecePixmaps[i]); painter.drawPixmap(pieceRects[i], piecePixmaps[i]);
}
painter.end(); painter.end();
} }

View File

@ -86,4 +86,4 @@ private:
int m_ImageSize; int m_ImageSize;
}; };
#endif #endif // PUZZLEWIDGET_H