Updated a lot of code snippets on documentation concerning QtCore module.
Reviewed by Hugo Parente <hugo.lima@openbossa.org> Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
This commit is contained in:
parent
59b4f2b59d
commit
7ee87cc8f2
29 changed files with 172 additions and 187 deletions
|
|
@ -1,18 +1,18 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
label->setText("Password:");
|
label.setText("Password:")
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
label->setText(tr("Password:"));
|
label.setText(QObject.tr("Password:"))
|
||||||
//! [1]
|
//! [1]
|
||||||
|
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
QFile file(QString::fromLatin1("appicon.png"));
|
file_ = QFile("appicon.png")
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
|
|
||||||
//! [3]
|
//! [3]
|
||||||
QFile file(QLatin1String("appicon.png"));
|
file_= QFile("appicon.png")
|
||||||
//! [3]
|
//! [3]
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,25 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
encodedString = QByteArray("...")
|
encodedString = QByteArray("...")
|
||||||
codec = QTextCodec::codecForName("KOI8-R")
|
codec = QTextCodec.codecForName("KOI8-R")
|
||||||
string = codec->toUnicode(encodedString)
|
string = codec.toUnicode(encodedString)
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
string = QString("...")
|
string = u"..."
|
||||||
codec = QTextCodec::codecForName("KOI8-R")
|
codec = QTextCodec.codecForName("KOI8-R")
|
||||||
encodedString = codec->fromUnicode(string)
|
encodedString = codec.fromUnicode(string)
|
||||||
//! [1]
|
//! [1]
|
||||||
|
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
codec = QTextCodec::codecForName("Shift-JIS")
|
codec = QTextCodec.codecForName("Shift-JIS")
|
||||||
decoder = codec->makeDecoder()
|
decoder = codec.makeDecoder()
|
||||||
|
|
||||||
string = QString()
|
string = u''
|
||||||
while new_data_available():
|
while new_data_available():
|
||||||
chunk = get_new_data();
|
chunk = get_new_data()
|
||||||
string += decoder->toUnicode(chunk)
|
string += decoder.toUnicode(chunk)
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
|
|
@ -27,6 +27,6 @@ while new_data_available():
|
||||||
//! [3]
|
//! [3]
|
||||||
def main():
|
def main():
|
||||||
app = QApplication([])
|
app = QApplication([])
|
||||||
QTextCodec::setCodecForTr(QTextCodec::codecForName("eucKR"))
|
QTextCodec.setCodecForTr(QTextCodec.codecForName("eucKR"))
|
||||||
...
|
...
|
||||||
//! [3]
|
//! [3]
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,5 @@ class HelloWorldTask(QRunnable):
|
||||||
|
|
||||||
hello = HelloWorldTask()
|
hello = HelloWorldTask()
|
||||||
# QThreadPool takes ownership and deletes 'hello' automatically
|
# QThreadPool takes ownership and deletes 'hello' automatically
|
||||||
QThreadPool.globalInstance()->start(hello)
|
QThreadPool.globalInstance().start(hello)
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
|
label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -73,60 +73,58 @@ quint64 value = Q_UINT64_C(932838457459459);
|
||||||
|
|
||||||
|
|
||||||
//! [10]
|
//! [10]
|
||||||
int absoluteValue;
|
myValue = -4
|
||||||
int myValue = -4;
|
absoluteValue = qAbs(myValue)
|
||||||
|
|
||||||
absoluteValue = qAbs(myValue);
|
|
||||||
// absoluteValue == 4
|
// absoluteValue == 4
|
||||||
//! [10]
|
//! [10]
|
||||||
|
|
||||||
|
|
||||||
//! [11]
|
//! [11]
|
||||||
qreal valueA = 2.3;
|
valueA = 2.3
|
||||||
qreal valueB = 2.7;
|
valueB = 2.7
|
||||||
|
|
||||||
int roundedValueA = qRound(valueA);
|
roundedValueA = qRound(valueA)
|
||||||
// roundedValueA = 2
|
// roundedValueA = 2
|
||||||
int roundedValueB = qRound(valueB);
|
roundedValueB = qRound(valueB)
|
||||||
// roundedValueB = 3
|
// roundedValueB = 3
|
||||||
//! [11]
|
//! [11]
|
||||||
|
|
||||||
|
|
||||||
//! [12]
|
//! [12]
|
||||||
qreal valueA = 42949672960.3;
|
valueA = 42949672960.3
|
||||||
qreal valueB = 42949672960.7;
|
valueB = 42949672960.7
|
||||||
|
|
||||||
int roundedValueA = qRound(valueA);
|
roundedValueA = qRound(valueA)
|
||||||
// roundedValueA = 42949672960
|
// roundedValueA = 42949672960
|
||||||
int roundedValueB = qRound(valueB);
|
roundedValueB = qRound(valueB)
|
||||||
// roundedValueB = 42949672961
|
// roundedValueB = 42949672961
|
||||||
//! [12]
|
//! [12]
|
||||||
|
|
||||||
|
|
||||||
//! [13]
|
//! [13]
|
||||||
int myValue = 6;
|
myValue = 6
|
||||||
int yourValue = 4;
|
yourValue = 4
|
||||||
|
|
||||||
int minValue = qMin(myValue, yourValue);
|
minValue = qMin(myValue, yourValue)
|
||||||
// minValue == yourValue
|
// minValue == yourValue
|
||||||
//! [13]
|
//! [13]
|
||||||
|
|
||||||
|
|
||||||
//! [14]
|
//! [14]
|
||||||
int myValue = 6;
|
myValue = 6
|
||||||
int yourValue = 4;
|
yourValue = 4
|
||||||
|
|
||||||
int maxValue = qMax(myValue, yourValue);
|
maxValue = qMax(myValue, yourValue)
|
||||||
// maxValue == myValue
|
// maxValue == myValue
|
||||||
//! [14]
|
//! [14]
|
||||||
|
|
||||||
|
|
||||||
//! [15]
|
//! [15]
|
||||||
int myValue = 10;
|
myValue = 10
|
||||||
int minValue = 2;
|
minValue = 2
|
||||||
int maxValue = 6;
|
maxValue = 6
|
||||||
|
|
||||||
int boundedValue = qBound(minValue, myValue, maxValue);
|
boundedValue = qBound(minValue, myValue, maxValue)
|
||||||
// boundedValue == 6
|
// boundedValue == 6
|
||||||
//! [15]
|
//! [15]
|
||||||
|
|
||||||
|
|
@ -436,7 +434,7 @@ namespace QT_NAMESPACE {
|
||||||
//! [43]
|
//! [43]
|
||||||
class MyClass : public QObject
|
class MyClass : public QObject
|
||||||
{
|
{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Q_DISABLE_COPY(MyClass)
|
Q_DISABLE_COPY(MyClass)
|
||||||
};
|
};
|
||||||
|
|
@ -446,7 +444,7 @@ class MyClass : public QObject
|
||||||
//! [44]
|
//! [44]
|
||||||
class MyClass : public QObject
|
class MyClass : public QObject
|
||||||
{
|
{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MyClass(const MyClass &);
|
MyClass(const MyClass &);
|
||||||
MyClass &operator=(const MyClass &);
|
MyClass &operator=(const MyClass &);
|
||||||
|
|
@ -454,7 +452,7 @@ class MyClass : public QObject
|
||||||
//! [44]
|
//! [44]
|
||||||
|
|
||||||
//! [45]
|
//! [45]
|
||||||
QWidget w = QWidget();
|
w = QWidget()
|
||||||
//! [45]
|
//! [45]
|
||||||
|
|
||||||
//! [46]
|
//! [46]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
class ZipEngineHandler(QAbstractFileEngineHandler)
|
class ZipEngineHandler(QAbstractFileEngineHandler):
|
||||||
def create(fileName):
|
def create(self, fileName):
|
||||||
# ZipEngineHandler returns a ZipEngine for all .zip files
|
# ZipEngineHandler returns a ZipEngine for all .zip files
|
||||||
if fileName.toLower().endsWith(".zip"):
|
if fileName.toLower().endsWith(".zip"):
|
||||||
return ZipEngine(fileName)
|
return ZipEngine(fileName)
|
||||||
|
|
@ -22,7 +22,7 @@ def main():
|
||||||
//! [1]
|
//! [1]
|
||||||
def create(fileName):
|
def create(fileName):
|
||||||
# ZipEngineHandler returns a ZipEngine for all .zip files
|
# ZipEngineHandler returns a ZipEngine for all .zip files
|
||||||
if fileName.toLower().endsWith(".zip"):
|
if fileName.lower().endswith(".zip"):
|
||||||
return ZipEngine(fileName)
|
return ZipEngine(fileName)
|
||||||
else
|
else
|
||||||
return None
|
return None
|
||||||
|
|
@ -52,9 +52,9 @@ class CustomIterator(QAbstractFileEngineIterator):
|
||||||
return self.index < self.entries.size() - 1
|
return self.index < self.entries.size() - 1
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
if !self.hasNext():
|
if not self.hasNext():
|
||||||
return QString()
|
return None
|
||||||
index++
|
index += 1
|
||||||
return currentFilePath()
|
return currentFilePath()
|
||||||
|
|
||||||
def currentFileName(self):
|
def currentFileName(self):
|
||||||
|
|
|
||||||
|
|
@ -2,27 +2,27 @@ void wrapInFunction()
|
||||||
{
|
{
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
file = QFile("file.dat")
|
file_ = QFile("file.dat")
|
||||||
file.open(QIODevice.WriteOnly)
|
file_.open(QIODevice.WriteOnly)
|
||||||
# we will serialize the data into the file
|
# we will serialize the data into the file
|
||||||
out = QDataStream(file)
|
out = QDataStream(file_)
|
||||||
# serialize a string
|
# serialize a string
|
||||||
out << "the answer is"
|
out.writeQString("the answer is")
|
||||||
# serialize an integer
|
# serialize an integer
|
||||||
out.writeInt32(42)
|
out.writeInt32(42)
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
file = QFile("file.dat")
|
file_ = QFile("file.dat")
|
||||||
file.open(QIODevice.ReadOnly)
|
file_.open(QIODevice.ReadOnly)
|
||||||
# read the data serialized from the file
|
# read the data serialized from the file
|
||||||
i = QDataStream(file)
|
i = QDataStream(file_)
|
||||||
str = QString()
|
string = ''
|
||||||
a = 0
|
a = 0
|
||||||
# extract "the answer is" and 42
|
# extract "the answer is" and 42
|
||||||
i >> str
|
string = i.readQString()
|
||||||
a = i.readInt32()
|
a = i.readInt32()
|
||||||
//! [1]
|
//! [1]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -32,15 +32,15 @@ stream.setVersion(QDataStream.Qt_4_0)
|
||||||
|
|
||||||
|
|
||||||
//! [3]
|
//! [3]
|
||||||
file = QFile("file.xxx")
|
file_ = QFile("file.xxx")
|
||||||
file.open(QIODevice.WriteOnly)
|
file_.open(QIODevice.WriteOnly)
|
||||||
out = QDataStream(file)
|
out = QDataStream(file_)
|
||||||
|
|
||||||
# Write a header with a "magic number" and a version
|
# Write a header with a "magic number" and a version
|
||||||
out.writeInt32(0xA0B0C0D0)
|
out.writeInt32(0xA0B0C0D0)
|
||||||
out.writeInt32(123)
|
out.writeInt32(123)
|
||||||
|
|
||||||
out.setVersion(QDataStream.Qt_4_0);
|
out.setVersion(QDataStream.Qt_4_0)
|
||||||
|
|
||||||
// Write the data
|
// Write the data
|
||||||
out << lots_of_interesting_data
|
out << lots_of_interesting_data
|
||||||
|
|
@ -48,9 +48,9 @@ out << lots_of_interesting_data
|
||||||
|
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
file = QFile("file.xxx")
|
file_ = QFile("file.xxx")
|
||||||
file.open(QIODevice.ReadOnly)
|
file_.open(QIODevice.ReadOnly)
|
||||||
i = QDataStream(file)
|
i = QDataStream(file_)
|
||||||
|
|
||||||
// Read and check the header
|
// Read and check the header
|
||||||
magic = i.readInt32()
|
magic = i.readInt32()
|
||||||
|
|
@ -65,20 +65,20 @@ if version > 123:
|
||||||
return XXX_BAD_FILE_TOO_NEW
|
return XXX_BAD_FILE_TOO_NEW
|
||||||
|
|
||||||
if version <= 110:
|
if version <= 110:
|
||||||
in.setVersion(QDataStream.Qt_3_2)
|
in_.setVersion(QDataStream.Qt_3_2)
|
||||||
else:
|
else:
|
||||||
in.setVersion(QDataStream.Qt_4_0)
|
in_.setVersion(QDataStream.Qt_4_0)
|
||||||
|
|
||||||
// Read the data
|
// Read the data
|
||||||
in >> lots_of_interesting_data
|
in_ >> lots_of_interesting_data
|
||||||
if version >= 120:
|
if version >= 120:
|
||||||
in >> data_new_in_XXX_version_1_2
|
in_ >> data_new_in_XXX_version_1_2
|
||||||
in >> other_interesting_data
|
in_ >> other_interesting_data
|
||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
out = QDataStream(file)
|
out = QDataStream(file_)
|
||||||
out.setVersion(QDataStream.Qt_4_0)
|
out.setVersion(QDataStream.Qt_4_0)
|
||||||
//! [5]
|
//! [5]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,14 +28,14 @@ absolutePath = directory.absoluteFilePath("contents.txt")
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
dir = QDir("example")
|
dir = QDir("example")
|
||||||
if !dir.exists():
|
if not dir.exists():
|
||||||
print "Cannot find the example directory"
|
print "Cannot find the example directory"
|
||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
dir = QDir.root() # "/"
|
dir = QDir.root() # "/"
|
||||||
if !dir.cd("tmp"): # "/tmp"
|
if not dir.cd("tmp"): # "/tmp"
|
||||||
print "Cannot find the \"/tmp\" directory"
|
print "Cannot find the \"/tmp\" directory"
|
||||||
else:
|
else:
|
||||||
file = QFile(dir.filePath("ex1.txt")) # "/tmp/ex1.txt"
|
file = QFile(dir.filePath("ex1.txt")) # "/tmp/ex1.txt"
|
||||||
|
|
@ -83,7 +83,7 @@ if dir.isRoot():
|
||||||
|
|
||||||
|
|
||||||
//! [10]
|
//! [10]
|
||||||
// The current directory is "/usr/local"
|
# The current directory is "/usr/local"
|
||||||
d1 = QDir("/usr/local/bin")
|
d1 = QDir("/usr/local/bin")
|
||||||
d2 = QDir("bin")
|
d2 = QDir("bin")
|
||||||
if d1 == d2:
|
if d1 == d2:
|
||||||
|
|
@ -112,7 +112,7 @@ Q_INIT_RESOURCE(myapp);
|
||||||
|
|
||||||
|
|
||||||
//! [14]
|
//! [14]
|
||||||
def initMyResource():
|
def initMyResource():
|
||||||
Q_INIT_RESOURCE(myapp)
|
Q_INIT_RESOURCE(myapp)
|
||||||
|
|
||||||
class MyNamespace
|
class MyNamespace
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ info1.size() # returns 56201
|
||||||
info1.symLinkTarget() # returns "/opt/pretty++/bin/untabify"
|
info1.symLinkTarget() # returns "/opt/pretty++/bin/untabify"
|
||||||
|
|
||||||
info2 = QFileInfo(info1.symLinkTarget())
|
info2 = QFileInfo(info1.symLinkTarget())
|
||||||
info1.isSymLink() # returns false
|
info1.isSymLink() # returns False
|
||||||
info1.absoluteFilePath() # returns "/opt/pretty++/bin/untabify"
|
info1.absoluteFilePath() # returns "/opt/pretty++/bin/untabify"
|
||||||
info1.size() # returns 56201
|
info1.size() # returns 56201
|
||||||
|
|
||||||
|
|
@ -20,25 +20,25 @@ info1.size() # returns 56201
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
info1 = QFileInfo("C:\\Documents and Settings\\Bob\\untabify.lnk")
|
info1 = QFileInfo("C:\\Documents and Settings\\Bob\\untabify.lnk")
|
||||||
info1.isSymLink() # returns true
|
info1.isSymLink() # returns True
|
||||||
info1.absoluteFilePath() # returns "C:/Documents and Settings/Bob/untabify.lnk"
|
info1.absoluteFilePath() # returns "C:/Documents and Settings/Bob/untabify.lnk"
|
||||||
info1.size() # returns 743
|
info1.size() # returns 743
|
||||||
info1.symLinkTarget() # returns "C:/Pretty++/untabify"
|
info1.symLinkTarget() # returns "C:/Pretty++/untabify"
|
||||||
|
|
||||||
info2 = QFileInfo(info1.symLinkTarget())
|
info2 = QFileInfo(info1.symLinkTarget())
|
||||||
info1.isSymLink() # returns false
|
info1.isSymLink() # returns False
|
||||||
info1.absoluteFilePath() # returns "C:/Pretty++/untabify"
|
info1.absoluteFilePath() # returns "C:/Pretty++/untabify"
|
||||||
info1.size() # returns 63942
|
info1.size() # returns 63942
|
||||||
//! [1]
|
//! [1]
|
||||||
|
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
absolute = QString("/local/bin")
|
absolute = "/local/bin"
|
||||||
relative = QString("local/bin")
|
relative = "local/bin"
|
||||||
absFile = QFileInfo(absolute)
|
absFile = QFileInfo(absolute)
|
||||||
relFile = QFileInfo(relative)
|
relFile = QFileInfo(relative)
|
||||||
|
|
||||||
QDir.setCurrent(QDir::rootPath())
|
QDir.setCurrent(QDir.rootPath())
|
||||||
# absFile and relFile now point to the same file
|
# absFile and relFile now point to the same file
|
||||||
|
|
||||||
QDir.setCurrent("/tmp")
|
QDir.setCurrent("/tmp")
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
gzip = QProcess()
|
gzip = QProcess()
|
||||||
gzip.start("gzip", QStringList() << "-c")
|
gzip.start("gzip", ["-c"])
|
||||||
if !gzip.waitForStarted():
|
if not gzip.waitForStarted():
|
||||||
return false
|
return False
|
||||||
|
|
||||||
gzip.write("uncompressed data");
|
gzip.write("uncompressed data")
|
||||||
|
|
||||||
compressed = QByteArray()
|
compressed = QByteArray()
|
||||||
while gzip.waitForReadyRead():
|
while gzip.waitForReadyRead():
|
||||||
|
|
@ -21,7 +21,7 @@ def bytesAvailable(self):
|
||||||
//! [2]
|
//! [2]
|
||||||
file = QFile("box.txt")
|
file = QFile("box.txt")
|
||||||
if file.open(QFile.ReadOnly):
|
if file.open(QFile.ReadOnly):
|
||||||
buf = file.readLine(1024);
|
buf = file.readLine(1024)
|
||||||
if buf.size():
|
if buf.size():
|
||||||
# the line is available in buf
|
# the line is available in buf
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
@ -29,20 +29,20 @@ if file.open(QFile.ReadOnly):
|
||||||
|
|
||||||
//! [3]
|
//! [3]
|
||||||
def canReadLine(self):
|
def canReadLine(self):
|
||||||
return buffer.contains('\n') || QIODevice.canReadLine()
|
return buffer.contains('\n') or QIODevice.canReadLine()
|
||||||
//! [3]
|
//! [3]
|
||||||
|
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
def isExeFile(file):
|
def isExeFile(file_):
|
||||||
buf = file->peek(2);
|
buf = file_.peek(2)
|
||||||
if buf.size() == 2:
|
if buf.size() == 2:
|
||||||
return (buf[0] == 'M' && buf[1] == 'Z')
|
return buf[0] == 'M' and buf[1] == 'Z'
|
||||||
return false
|
return False
|
||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
def isExeFile(file):
|
def isExeFile(file_):
|
||||||
return file->peek(2) == "MZ"
|
return file_.peek(2) == "MZ"
|
||||||
//! [5]
|
//! [5]
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,11 @@ builder = QProcess()
|
||||||
builder.setProcessChannelMode(QProcess.MergedChannels)
|
builder.setProcessChannelMode(QProcess.MergedChannels)
|
||||||
builder.start("make", QStringList() << "-j2")
|
builder.start("make", QStringList() << "-j2")
|
||||||
|
|
||||||
|
import sys
|
||||||
if !builder.waitForFinished():
|
if !builder.waitForFinished():
|
||||||
qDebug() << "Make failed:" << builder.errorString()
|
sys.stderr.write("Make failed:" + builder.errorString())
|
||||||
else
|
else
|
||||||
qDebug() << "Make output:" << builder.readAll()
|
sys.stderr.write("Make output:" + builder.readAll())
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -106,8 +106,8 @@ settings.endGroup()
|
||||||
|
|
||||||
//! [15]
|
//! [15]
|
||||||
class Login:
|
class Login:
|
||||||
userName = QString()
|
userName = ''
|
||||||
password = QString()
|
password = ''
|
||||||
|
|
||||||
logins = []
|
logins = []
|
||||||
...
|
...
|
||||||
|
|
@ -127,8 +127,8 @@ class Login:
|
||||||
|
|
||||||
//! [16]
|
//! [16]
|
||||||
class Login:
|
class Login:
|
||||||
userName = QString()
|
userName = ''
|
||||||
password = QString()
|
password = ''
|
||||||
|
|
||||||
logins = []
|
logins = []
|
||||||
...
|
...
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
# Within a function/method...
|
# Within a function/method...
|
||||||
|
|
||||||
file = QTemporaryFile()
|
file_ = QTemporaryFile()
|
||||||
if file.open():
|
if file_.open():
|
||||||
# file.fileName() returns the unique file name
|
# file_.fileName() returns the unique file name
|
||||||
|
|
||||||
# The QTemporaryFile destructor removes the temporary file
|
# The QTemporaryFile destructor removes the temporary file
|
||||||
# as it goes out of scope.
|
# as it goes out of scope.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
data = QFile("output.txt")
|
data = QFile("output.txt")
|
||||||
if data.open(QFile.WriteOnly | QFile::Truncate):
|
if data.open(QFile.WriteOnly | QFile.Truncate):
|
||||||
out = QTextStream(&data)
|
out = QTextStream(&data)
|
||||||
out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7
|
out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7
|
||||||
# writes "Result: 3.14 2.7 "
|
# writes "Result: 3.14 2.7 "
|
||||||
|
|
@ -19,15 +19,15 @@ while(True):
|
||||||
|
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
in = QTextStream("0x50 0x20")
|
in_ = QTextStream("0x50 0x20")
|
||||||
firstNumber = 0
|
firstNumber = 0
|
||||||
secondNumber = 0
|
secondNumber = 0
|
||||||
|
|
||||||
in >> firstNumber # firstNumber == 80
|
in_ >> firstNumber # firstNumber == 80
|
||||||
in >> dec >> secondNumber # secondNumber == 0
|
in_ >> dec >> secondNumber # secondNumber == 0
|
||||||
|
|
||||||
ch = None
|
ch = None
|
||||||
in >> ch # ch == 'x'
|
in_ >> ch # ch == 'x'
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -36,16 +36,16 @@ def main():
|
||||||
# read numeric arguments (123, 0x20, 4.5...)
|
# read numeric arguments (123, 0x20, 4.5...)
|
||||||
for i in sys.argv():
|
for i in sys.argv():
|
||||||
number = None
|
number = None
|
||||||
QTextStream in(i)
|
QTextStream in_(i)
|
||||||
in >> number
|
in_ >> number
|
||||||
...
|
...
|
||||||
//! [3]
|
//! [3]
|
||||||
|
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
str = QString()
|
str = QString()
|
||||||
in = QTextStream(sys.stdin.fileno())
|
in_ = QTextStream(sys.stdin.fileno())
|
||||||
in >> str
|
in_ >> str
|
||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -65,11 +65,11 @@ out << "Qt" << "rocks!"
|
||||||
|
|
||||||
|
|
||||||
//! [7]
|
//! [7]
|
||||||
in = QTextStream(file)
|
in_ = QTextStream(file)
|
||||||
ch1 = QChar()
|
ch1 = QChar()
|
||||||
ch2 = QChar()
|
ch2 = QChar()
|
||||||
ch3 = QChar()
|
ch3 = QChar()
|
||||||
in >> ch1 >> ch2 >> ch3;
|
in_ >> ch1 >> ch2 >> ch3;
|
||||||
//! [7]
|
//! [7]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,10 @@ url = QUrl.fromEncoded("http://qtsoftware.com/List%20of%20holidays.xml")
|
||||||
//! [2]
|
//! [2]
|
||||||
def checkUrl(url):
|
def checkUrl(url):
|
||||||
if !url.isValid():
|
if !url.isValid():
|
||||||
print QString("Invalid URL: %1").arg(url.toString())
|
print "Invalid URL: %s" % url.toString()
|
||||||
return false
|
return False
|
||||||
|
|
||||||
return true
|
return True
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
|
|
@ -41,6 +41,6 @@ print baseUrl.resolved(relativeUrl).toString()
|
||||||
|
|
||||||
//! [6]
|
//! [6]
|
||||||
ba = QUrl.toPercentEncoding("{a fishy string?}", "{}", "s")
|
ba = QUrl.toPercentEncoding("{a fishy string?}", "{}", "s")
|
||||||
print ba.constData()
|
print ba
|
||||||
# prints "{a fi%73hy %73tring%3F}"
|
# prints "{a fi%73hy %73tring%3F}"
|
||||||
//! [6]
|
//! [6]
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ QApplication.sendEvent(mainWindow, event)
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
quitButton = QPushButton("Quit")
|
quitButton = QPushButton("Quit")
|
||||||
QObject.connect(quitButton, SIGNAL("clicked()"), app, SLOT("quit()"))
|
quitButton.clicked.connect(app.quit)
|
||||||
//! [1]
|
//! [1]
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -22,19 +22,11 @@ myEventFilter(message, result)
|
||||||
|
|
||||||
|
|
||||||
//! [4]
|
//! [4]
|
||||||
static int *global_ptr = 0;
|
def cleanup_stuff():
|
||||||
|
# do the cleanup stuff
|
||||||
|
|
||||||
static void cleanup_ptr()
|
def init_stuff():
|
||||||
{
|
qAddPostRoutine(cleanup_stuff)
|
||||||
delete [] global_ptr;
|
|
||||||
global_ptr = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void init_ptr()
|
|
||||||
{
|
|
||||||
global_ptr = new int[100]; // allocate data
|
|
||||||
qAddPostRoutine(cleanup_ptr); // delete later
|
|
||||||
}
|
|
||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,17 +48,13 @@ class MyClass:
|
||||||
|
|
||||||
//! [propertyCount]
|
//! [propertyCount]
|
||||||
metaObject = obj.metaObject()
|
metaObject = obj.metaObject()
|
||||||
properties = QStringList()
|
properties = [metaObject.property(i).name() for i in range(metaObject.propertyOffset(), metaObject.propertyCount())]
|
||||||
for i in range(metaObject.propertyOffset(), metaObject.propertyCount()):
|
|
||||||
properties << QString.fromLatin1(metaObject.property(i).name())
|
|
||||||
//! [propertyCount]
|
//! [propertyCount]
|
||||||
|
|
||||||
|
|
||||||
//! [methodCount]
|
//! [methodCount]
|
||||||
metaObject = obj.metaObject()
|
metaObject = obj.metaObject()
|
||||||
methods = QStringList()
|
methods = [metaObject.method(i).signature() for i in range(metaObject.methodOffset(), metaObject.methodCount())]
|
||||||
for i in rang(metaObject.methodOffset(), metaObject->methodCount()):
|
|
||||||
methods << QString.fromLatin1(metaObject.method(i).signature())
|
|
||||||
//! [methodCount]
|
//! [methodCount]
|
||||||
|
|
||||||
//! [6]
|
//! [6]
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,5 @@ def main():
|
||||||
app = QApplication([])
|
app = QApplication([])
|
||||||
QTimer.singleShot(600000, app, SLOT('quit()'))
|
QTimer.singleShot(600000, app, SLOT('quit()'))
|
||||||
...
|
...
|
||||||
return app.exec()
|
return app.exec_()
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,5 @@ class MyThread (QThread):
|
||||||
# connect QTcpSocket's signals somewhere meaningful
|
# connect QTcpSocket's signals somewhere meaningful
|
||||||
# ...
|
# ...
|
||||||
socket.connectToHost(hostName, portNumber)
|
socket.connectToHost(hostName, portNumber)
|
||||||
exec_()
|
self.exec_()
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,7 @@ c = a | b
|
||||||
|
|
||||||
//! [14]
|
//! [14]
|
||||||
a = QBitArray(3)
|
a = QBitArray(3)
|
||||||
QBitArray b(2)
|
b = QBitArray(2)
|
||||||
c = QBitArray()
|
c = QBitArray()
|
||||||
a[0] = 1
|
a[0] = 1
|
||||||
a[1] = 0
|
a[1] = 0
|
||||||
|
|
|
||||||
|
|
@ -327,15 +327,13 @@ ba = QByteArray.number(12.3456, 'E', 3)
|
||||||
|
|
||||||
|
|
||||||
//! [43]
|
//! [43]
|
||||||
static const char mydata[] = {
|
mydata = '\x00\x00\x03\x84\x78\x9c\x3b\x76'\
|
||||||
0x00, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76,
|
'\xec\x18\xc3\x31\x0a\xf1\xcc\x99'\
|
||||||
0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99,
|
...
|
||||||
...
|
'\x6d\x5b'
|
||||||
0x6d, 0x5b
|
|
||||||
};
|
|
||||||
|
|
||||||
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
|
data = QByteArray.fromRawData(mydata)
|
||||||
QDataStream in(&data, QIODevice::ReadOnly);
|
in_ = QDataStream(data, QIODevice.ReadOnly)
|
||||||
...
|
...
|
||||||
//! [43]
|
//! [43]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ QDate.isValid(1202, 6, 6) # True (even though 1202 is pre-Gregorian)
|
||||||
|
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
n = QTime(14, 0, 0) # n == 14:00:00
|
n = QTime(14, 0, 0) # n == 14:00:00
|
||||||
t = QTime()
|
t = QTime()
|
||||||
t = n.addSecs(70) # t == 14:01:10
|
t = n.addSecs(70) # t == 14:01:10
|
||||||
t = n.addSecs(-70) # t == 13:58:50
|
t = n.addSecs(-70) # t == 13:58:50
|
||||||
|
|
|
||||||
|
|
@ -14,15 +14,18 @@ hebrew = QLocale() # Constructs a default QLocale
|
||||||
s1 = hebrew.toString(15714.3, 'e')
|
s1 = hebrew.toString(15714.3, 'e')
|
||||||
|
|
||||||
QLocale.setDefault(QLocale(QLocale.C))
|
QLocale.setDefault(QLocale(QLocale.C))
|
||||||
(d, ok) = QString("1234,56").toDouble() # ok == false
|
c = QLocale()
|
||||||
(d, ok) = QString("1234.56").toDouble() # ok == true, d == 1234.56
|
(d, ok) = c.toDouble("1234,56") # ok == false
|
||||||
|
(d, ok) = c.toDouble("1234.56") # ok == true, d == 1234.56
|
||||||
|
|
||||||
QLocale.setDefault(QLocale(QLocale.German))
|
QLocale.setDefault(QLocale(QLocale.German))
|
||||||
(d, ok) = QString("1234,56").toDouble() # ok == true, d == 1234.56
|
german = QLocale()
|
||||||
(d, ok) = QString("1234.56").toDouble() # ok == true, d == 1234.56
|
(d, ok) = german.toDouble("1234,56") # ok == true, d == 1234.56
|
||||||
|
(d, ok) = german.toDouble("1234.56") # ok == true, d == 1234.56
|
||||||
|
|
||||||
QLocale.setDefault(QLocale(QLocale.English, QLocale.UnitedStates))
|
QLocale.setDefault(QLocale(QLocale.English, QLocale.UnitedStates))
|
||||||
string = QString("%1 %L2 %L3").arg(12345).arg(12345).arg(12345, 0, 16)
|
english = QLocale()
|
||||||
|
string = '%s %s %10x' % (12345, english.toString(12345), 12345)
|
||||||
# string == "12345 12,345 3039"
|
# string == "12345 12,345 3039"
|
||||||
//! [1]
|
//! [1]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,10 @@ progressBar.setRange(0, 100)
|
||||||
# Construct a 1-second timeline with a frame range of 0 - 100
|
# Construct a 1-second timeline with a frame range of 0 - 100
|
||||||
timeLine = QTimeLine(1000, self)
|
timeLine = QTimeLine(1000, self)
|
||||||
timeLine.setFrameRange(0, 100)
|
timeLine.setFrameRange(0, 100)
|
||||||
QObject.connect(timeLine, SIGNAL("frameChanged(int)"), progressBar, SLOT("setValue(int)"))
|
timeLine.frameChanged[int].connect(progressBar.setValue)
|
||||||
|
|
||||||
# Clicking the push button will start the progress bar animation
|
# Clicking the push button will start the progress bar animation
|
||||||
pushButton = QPushButton(QObject.tr("Start animation"), self)
|
pushButton = QPushButton(QObject.tr("Start animation"), self)
|
||||||
QObject.connect(pushButton, SIGNAL("clicked()"), timeLine, SLOT("start()"))
|
pushButton.clicked.connect(timeLine.start)
|
||||||
...
|
...
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ bool zip()
|
||||||
{
|
{
|
||||||
//! [0]
|
//! [0]
|
||||||
gzip = QProcess()
|
gzip = QProcess()
|
||||||
gzip.start("gzip", QStringList() << "-c")
|
gzip.start("gzip", ["-c"])
|
||||||
if not gzip.waitForStarted():
|
if not gzip.waitForStarted():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,28 +39,21 @@
|
||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QDir>
|
from PySide.QtCore import *
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
def main():
|
||||||
{
|
dir_ = QDir()
|
||||||
QDir dir;
|
dir_.setFilter(QDir.Files | QDir.Hidden | QDir.NoSymLinks)
|
||||||
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
|
dir_.setSorting(QDir.Size | QDir.Reversed)
|
||||||
dir.setSorting(QDir::Size | QDir::Reversed);
|
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
filters = QStringList()
|
filters = ["*.cpp", "*.cxx", "*.cc"]
|
||||||
filters << "*.cpp" << "*.cxx" << "*.cc"
|
dir_.setNameFilters(filters)
|
||||||
dir.setNameFilters(filters)
|
|
||||||
//! [0]
|
//! [0]
|
||||||
|
//
|
||||||
|
lst = d.entryInfoList()
|
||||||
|
|
||||||
|
print " Bytes Filename"
|
||||||
|
for fileInfo in lst:
|
||||||
|
print '%d %s' % (fileInfo.size(), fileInfo.fileName())
|
||||||
|
|
||||||
QFileInfoList list = dir.entryInfoList();
|
|
||||||
std::cout << " Bytes Filename" << std::endl;
|
|
||||||
for (int i = 0; i < list.size(); ++i) {
|
|
||||||
QFileInfo fileInfo = list.at(i);
|
|
||||||
std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
|
|
||||||
.arg(fileInfo.fileName()));
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,15 @@
|
||||||
void startProcess()
|
void startProcess()
|
||||||
{
|
{
|
||||||
//! [0]
|
//! [0]
|
||||||
|
import re
|
||||||
|
from PySide.QtCore import QProcess
|
||||||
|
|
||||||
process = QProcess()
|
process = QProcess()
|
||||||
|
|
||||||
env = QProcess.systemEnvironment()
|
env = QProcess.systemEnvironment()
|
||||||
env << "TMPDIR=C:\\MyApp\\temp" # Add an environment variable
|
env.append("TMPDIR=C:\\MyApp\\temp") # Add an environment variable
|
||||||
env.replaceInStrings(QRegExp("^PATH=(.*)", Qt.CaseInsensitive), "PATH=\\1;C:\\Bin")
|
regex = re.compile(r'^PATH=(.*)', re.IGNORECASE)
|
||||||
|
env = [regex.sub(r'PATH=\1;C:\\Bin', var) for var in env]
|
||||||
process.setEnvironment(env)
|
process.setEnvironment(env)
|
||||||
process.start("myapp")
|
process.start("myapp")
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
|
||||||
|
|
@ -51,13 +51,12 @@ int main(int argc, char *argv[])
|
||||||
parent = &app;
|
parent = &app;
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
program = QString("./path/to/Qt/examples/widgets/analogclock")
|
program = "./path/to/Qt/examples/widgets/analogclock"
|
||||||
//! [1]
|
//! [1]
|
||||||
program = "./../../../../examples/widgets/analogclock/analogclock";
|
program = "./../../../../examples/widgets/analogclock/analogclock";
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
arguments = QStringList()
|
arguments ["-style", "motif"]
|
||||||
arguments << "-style" << "motif"
|
|
||||||
|
|
||||||
myProcess = QProcess(parent)
|
myProcess = QProcess(parent)
|
||||||
myProcess.start(program, arguments)
|
myProcess.start(program, arguments)
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@
|
||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QTimer>
|
from PySide.QtCore import QTimer
|
||||||
|
|
||||||
class Foo : public QObject
|
class Foo : public QObject
|
||||||
{
|
{
|
||||||
|
|
@ -52,7 +52,7 @@ Foo::Foo()
|
||||||
//! [0]
|
//! [0]
|
||||||
timer = QTimer(self)
|
timer = QTimer(self)
|
||||||
//! [0] //! [1]
|
//! [0] //! [1]
|
||||||
self.connect(timer, SIGNAL("timeout()"), self.updateCaption)
|
timer.timeout.connect(self.updateCaption)
|
||||||
//! [1] //! [2]
|
//! [1] //! [2]
|
||||||
timer.start(1000)
|
timer.start(1000)
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
@ -66,7 +66,7 @@ Foo::Foo()
|
||||||
//! [4]
|
//! [4]
|
||||||
timer = QTimer(self)
|
timer = QTimer(self)
|
||||||
//! [4] //! [5]
|
//! [4] //! [5]
|
||||||
self.connect(timer, SIGNAL("timeout()"), self.processOneThing)
|
timer.timeout.connect(self.processOneThing)
|
||||||
//! [5] //! [6]
|
//! [5] //! [6]
|
||||||
timer.start()
|
timer.start()
|
||||||
//! [6]
|
//! [6]
|
||||||
|
|
@ -75,5 +75,5 @@ Foo::Foo()
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
//! [0]
|
//! [0]
|
||||||
Act = QAction(tr("&New"), self)
|
Act = QAction(tr("&New"), self)
|
||||||
Act.setShortcut(tr("Ctrl+N"))
|
Act.setShortcut(tr("Ctrl+N"))
|
||||||
Act.setStatusTip(tr("Create a new file"))
|
Act.setStatusTip(QObject.tr("Create a new file"))
|
||||||
Act.setWhatsThis(tr("Click self option to create a new file."))
|
Act.setWhatsThis(QObject.tr("Click self option to create a new file."))
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue