Skip to content

Commit

Permalink
Added more code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
afester committed Nov 5, 2012
1 parent 6b3142b commit 3c312c6
Show file tree
Hide file tree
Showing 20 changed files with 464 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Opacity/MainWindow.cpp
@@ -0,0 +1,62 @@
/**
* This work is licensed under the Creative Commons Attribution 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* StackOverflow reference:
* http://stackoverflow.com/questions/12307119/qgraphicssceneitems-returns-no-items-for-a-given-rect
*/

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QDebug>
#include <QKeyEvent>
#include <QGraphicsPixmapItem>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
, scene(new QGraphicsScene())
{
ui->setupUi(this);

ui->graphicsView->setScene(scene);
ui->graphicsView->installEventFilter(this);
ui->graphicsView->show();

for (int y = 0; y < 20; ++y) {
for (int x = 0; x < 20; ++x) {
QPixmap pixmap("desert.png");
QGraphicsPixmapItem* newItem(scene->addPixmap(pixmap));
newItem->setPos(x * 25, y * 25);
newItem->setOpacity(0.01);
qDebug() << newItem << newItem->isVisible();
}
}
}

MainWindow::~MainWindow()
{
delete ui;
delete scene;
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Space) {
QRectF rect(0, 0, 25 * 3, 25 * 3);
QList<QGraphicsItem*> items(scene->items(rect, Qt::ContainsItemBoundingRect, Qt::AscendingOrder));
qDebug() << items;
foreach (QGraphicsItem *item, items) {
item->setOpacity(1.0);
}
}
return true;
}
return QObject::eventFilter(obj, event);
}
29 changes: 29 additions & 0 deletions Opacity/MainWindow.h
@@ -0,0 +1,29 @@
/**
* This work is licensed under the Creative Commons Attribution 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* StackOverflow reference:
* http://stackoverflow.com/questions/12307119/qgraphicssceneitems-returns-no-items-for-a-given-rect
*/

#include <QMainWindow>
#include <QWidget>
#include <QEvent>
#include <QGraphicsScene>
#include "ui_MainWindow.h"

class MainWindow : public QMainWindow {
Q_OBJECT

public:

MainWindow(QWidget* parent);
~MainWindow();
bool eventFilter(QObject *obj, QEvent *event);

Ui_MainWindow* ui;
QGraphicsScene* scene;
};
37 changes: 37 additions & 0 deletions Opacity/MainWindow.ui
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGraphicsView" name="graphicsView"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
12 changes: 12 additions & 0 deletions Opacity/Opacity.pro
@@ -0,0 +1,12 @@
# This work is licensed under the Creative Commons Attribution 3.0 Unported
# License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
# Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

TEMPLATE = app
QT = gui core

CONFIG += qt warn_on
FORMS = MainWindow.ui
HEADERS = MainWindow.h
SOURCES = MainWindow.cpp main.cpp
Binary file added Opacity/desert.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Opacity/main.cpp
@@ -0,0 +1,23 @@
/**
* This work is licensed under the Creative Commons Attribution 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* StackOverflow reference:
* http://stackoverflow.com/questions/12307119/qgraphicssceneitems-returns-no-items-for-a-given-rect
*/

#include "MainWindow.h"

int main(int argc, char ** argv) {

QApplication app( argc, argv );

MainWindow win(0);
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );

return app.exec();
}
9 changes: 9 additions & 0 deletions QDataStream/Datastream.pro
@@ -0,0 +1,9 @@
# This work is licensed under the Creative Commons Attribution 3.0 Unported
# License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
# Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

TEMPLATE = app
QT = core
CONFIG += console qt warn_on
SOURCES = main.cpp
49 changes: 49 additions & 0 deletions QDataStream/main.cpp
@@ -0,0 +1,49 @@
/**
* This work is licensed under the Creative Commons Attribution 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* StackOverflow reference:
* http://stackoverflow.com/questions/12501140/qt-qstring-from-qdatastream
*/

#include <QCoreApplication>
#include <QDebug>
#include <QDataStream>
#include <QBuffer>

void myFunc(QDataStream& in)
{
quint8 v;
in >> v;
qDebug() << v;
// Ok, I caught v value successfuly
QString s;
in >> s;
qDebug() << s;
// Didnt work :<
}


int main(int argc, char ** argv) {
QCoreApplication a(argc, argv);

QBuffer buffer;
buffer.open(QBuffer::ReadWrite);

// write test data into the biffer
QDataStream out(&buffer);
quint8 ival = 42;
QString sval = "Qt";
out << ival;
out << sval;

// read back data
buffer.seek(0);
myFunc(out);


return a.exec();
}
42 changes: 42 additions & 0 deletions QTextEdit/MainWindow.cpp
@@ -0,0 +1,42 @@
/**
* This work is licensed under the Creative Commons Attribution 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* StackOverflow reference:
* http://stackoverflow.com/questions/12606365/how-to-match-tab-length-to-a-number-of-spaces-in-qt
*/

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QDebug>
#include <QKeyEvent>
#include <QGraphicsPixmapItem>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {

ui->setupUi(this);
ui->textEdit->setFontFamily("Courier New");
QFontMetrics fm(ui->textEdit->currentFont());
int spaceWidth = fm.width(" ");
int tabStop = spaceWidth * 4;
ui->textEdit->setTabStopWidth(tabStop + 1);
ui->textEdit->setText("\tAfter one tab\n"
" After 4 spaces\n"
"\t\tAfter two tabs\n"
" After 8 spaces\n"
"\t\t\t\t\t\tAfter six tabs\n"
" After 24 spaces\n"
"\t\t\t\t\t\t\t\t\t\t\t\tAfter 12 tabs\n"
" After 48 spaces\n");
}

MainWindow::~MainWindow()
{
delete ui;
}
25 changes: 25 additions & 0 deletions QTextEdit/MainWindow.h
@@ -0,0 +1,25 @@
/**
* This work is licensed under the Creative Commons Attribution 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* StackOverflow reference:
* http://stackoverflow.com/questions/12606365/how-to-match-tab-length-to-a-number-of-spaces-in-qt
*/

#include <QMainWindow>
#include <QWidget>
#include "ui_MainWindow.h"

class MainWindow : public QMainWindow {
Q_OBJECT

public:

MainWindow(QWidget* parent);
~MainWindow();

Ui_MainWindow* ui;
};
37 changes: 37 additions & 0 deletions QTextEdit/MainWindow.ui
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
11 changes: 11 additions & 0 deletions QTextEdit/Textedit.pro
@@ -0,0 +1,11 @@
# This work is licensed under the Creative Commons Attribution 3.0 Unported
# License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
# Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

TEMPLATE = app
QT = gui core
CONFIG += qt warn_on
FORMS = MainWindow.ui
HEADERS = MainWindow.h
SOURCES = MainWindow.cpp main.cpp
23 changes: 23 additions & 0 deletions QTextEdit/main.cpp
@@ -0,0 +1,23 @@
/**
* This work is licensed under the Creative Commons Attribution 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/* StackOverflow reference:
* http://stackoverflow.com/questions/12606365/how-to-match-tab-length-to-a-number-of-spaces-in-qt
*/

#include "MainWindow.h"

int main(int argc, char ** argv) {

QApplication app( argc, argv );

MainWindow win(0);
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );

return app.exec();
}
5 changes: 5 additions & 0 deletions qmakeConflict/CMakeLists.txt
@@ -0,0 +1,5 @@
cmake_minimum_required (VERSION 2.6)
project (conflict)
add_executable(conflict foo/conflict.cpp bar/conflict.cpp main.cpp)
include_directories("/usr/include/qt4/QtGui" "/usr/include/qt4")

11 changes: 11 additions & 0 deletions qmakeConflict/Conflict.pro
@@ -0,0 +1,11 @@
# This work is licensed under the Creative Commons Attribution 3.0 Unported
# License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative
# Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

TEMPLATE = app
QT = gui core
CONFIG += qt warn_on

HEADERS = foo/conflict.h bar/conflict.h
SOURCES = foo/conflict.cpp bar/conflict.cpp

0 comments on commit 3c312c6

Please sign in to comment.