السلام عليكــم ورحمـة الله وبركاتــة على بركة الله ابدء معكم دروس بسيطة لتعلم ال Qt . طبعا المستوى مبتدئ ,, لأني انا مبتدئ. اتمنى من الاعضاء الخبراء ان يتواجدوا هنا و ان يصححوا لي فأحتمال الخطأ وارد جدا. الهدف من الموضوع ان نتعلم معا .. وكل من لديه معلومة يضيفها. بسم الله نبدأ. يفترض انك منصب متطلبات العمل. راجع الشرح هنا http://qt-ar.org/lessons/show3.html هذه هي واجهة ال QDevelop و التي سنعمل عليها
** Cplusplus GUI Programming with Qt4 (2nd Edition).chmThe connect() statement looks like this:connect(sender, SIGNAL(signal), receiver, SLOT(slot));where sender and receiver are pointers to QObjects and where signal and slot are function signatures without parameter names. The SIGNAL() and SLOT() macros essentially convert their argument to a string.** No.Starch.Press.The.Book.of.Qt.4.The.Art.of.Building.Qt.Applications.Jul.2007The first two arguments specify the object sending the signal and the signal that we want to bind to the receiving slot. The last two arguments specify the object that is the recipient of the signal, and the receiving slot.
3 Buttons2 Labels2 Line Edit
#include "dialog.h"Dialog::Dialog(QWidget *parent, Qt::WFlags flags) : QDialog(parent, flags){ ui.setupUi(this); connect(ui.textButton ,SIGNAL(clicked()), this , SLOT(InputText())); connect(ui.intButton,SIGNAL(clicked()), this , SLOT(InputNum())); connect(ui.itemButton ,SIGNAL(clicked()), this , SLOT(ChooseItem()));}Dialog::~Dialog(){}void Dialog::InputText(){ bool ok; QString text = QInputDialog::getText( this, tr("string"), tr("Enter your name:"), QLineEdit::Normal, tr("Alingsas"), &ok); if ( ok && !text.isEmpty()) QMessageBox::information(this,"Input", " Hello " + text);}void Dialog::InputNum(){ bool ok; QStringList items; items << tr("foo") << tr("Bar") << tr("Baz"); QString item = QInputDialog::getItem( this, tr("Item"), tr("Pick an item:"), items, 0, false, &ok); if ( ok && !item.isEmpty()) QMessageBox::information(this,"Input", item);}void Dialog::ChooseItem(){ bool ok; int value = QInputDialog::getInteger( this, tr("Integer"), tr("Enter an angle."), 90, 0, 360, 1, &ok); if(ok) QMessageBox::information(this,"Input",QString::number(value));}
#include "commonDlg.h"//CommonDlg::CommonDlg( QWidget * parent, Qt::WFlags f) : QDialog(parent, f){setupUi(this);connect(openButton,SIGNAL(clicked()), this , SLOT(Open()));connect(saveButton,SIGNAL(clicked()), this , SLOT(Save()));connect(opnDirButton ,SIGNAL(clicked()), this , SLOT(OpenDir()));connect(colorButton,SIGNAL(clicked()), this , SLOT(SetColor()));connect(fontButton,SIGNAL(clicked()), this , SLOT(SetFont()));}//void CommonDlg::Open(){QString filename = QFileDialog::getOpenFileName( this,tr("Open Document"),QDir::currentPath(),tr("Document files (*.doc *.rtf);; All files(*.*)"));if (!filename.isNull())QMessageBox::information(this , "Open ",filename);}void CommonDlg::Save(){QString filename = QFileDialog::getSaveFileName( this,tr("Save Document"),QDir::currentPath(),tr("Documents (*.doc)"));}void CommonDlg::OpenDir(){QString filename = QFileDialog::getExistingDirectory( this,tr("Select a Directory"),QDir::currentPath());if (!filename.isNull())QMessageBox::information(this , "Open Dir ",filename);}void CommonDlg::SetColor(){QColor color = QColorDialog::getColor(Qt::yellow,this );if (color.isValid() ){}}void CommonDlg::SetFont(){bool ok;QFont font = QFontDialog::getFont(&ok,QFont ( " Arial", 18 ),this,tr("Pick a font"));if(ok){}}
void DialogCalc::calcResult(){long Num1,Num2;Num1 = txtNum1->text().toLong();Num2 = txtNum2->text().toLong();if(comboBox->itemText(comboBox->currentIndex()) =="Add")QMessageBox::information(this ,"Result",QString::number(Num1 + Num2));else if(comboBox->itemText(comboBox->currentIndex()) =="Sub")QMessageBox::information(this ,"Result",QString::number(Num1 - Num2));else if(comboBox->itemText(comboBox->currentIndex()) =="Mul")QMessageBox::information(this ,"Result",QString::number(Num1 * Num2));elseif(comboBox->itemText(comboBox->currentIndex()) =="Div")QMessageBox::information(this ,"Result",QString::number(Num1 / Num2));}
long QString::toLong ( bool * ok = 0, int base = 10 ) constReturns the string converted to a long using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.Example: QString str = "FF"; bool ok; long hex = str.toLong(&ok, 16); // hex == 255, ok == true long dec = str.toLong(&ok, 10); // dec == 0, ok == false
We use the QIODevice::Text flag so that the editor can cope with the differences between Unix and Windows with respect to text files. Unix uses just a line feed (\n) to separate lines, whereas Windows in addition requires the control character for a carriage return (\r\n). Qt classes are internally based on Unix conventions wherever possible, which is why QTextEdit only works with line feeds, and so we have QFile remove all the carriage returns when it opens a text file on Windows platforms by specifying QIODevice::Text.Now the readAll() method reads the entire contents of the file into a QByteArray. We could import this directly into the textWidget, using setPlainText(), but we do not knowthe encoding format of the files. QByteArray contains the text in its 8-bit encoding, while QString uses 16-bit Unicode characters. In Windows, text files are normally saved in UTF-8 format. This mirrors the Unicode characters in 8-bits, and is compatible to ASCII encoding. In Linux, text files are available either as UTF-8 or in country-specific encoding, such as ISO Latin 1 (also known as ISO 8859-1). For the sake of simplicity, this application assumes that files are always encoded in UTF-8 and therefore converts the text contents using QString::fromUtf8() into a QString.(.The.Art.of.Building.Qt.Applications.)