capture QKeySequence from QKeyEvent

if (event->type() == QEvent::KeyPress){ QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); int keyInt = keyEvent->key(); Qt::Key key = static_cast<Qt::Key>(keyInt); if(key == Qt::Key_unknown){     qDebug() << “Unknown key from a macro probably”;     return; } // the user have clicked just and only the special keys Ctrl, Shift, Alt, Meta. if(key == Qt::Key_Control ||     key == Qt::Key_Shift ||    … Read more →

QShortcut , QKeySequence and variable name as SLOT example code

QShortcut , QKeySequence and variable name as SLOT example code QShortcut *m_shortcutHiddenFunct = new QShortcut(QKeySequence(tr(“Ctrl+M”)),this); // m_shortcutHiddenFunct->setKey(key); connect(m_shortcutHiddenFunct,SIGNAL(activated()) ,this, SLOT(on_shortcut_HiddenFunc_triggered())); // m_shortcutHiddenFunct->setKey( QKeySequence(tr(“Ctrl+m”))); QHash< QString, QHash > mapper; QHash bookmark; bookmark.insert(“Next”,”Ctrl+N”); bookmark.insert(“Previous”,”Ctrl+J”); mapper.insert(“bookmark”,bookmark); QHash editor; editor.insert(“paste”,”Ctrl+V”); editor.insert(“cut”,”Ctrl+X”); mapper.insert(“editor”,editor); QHashIterator i(mapper); while (i.hasNext()) { i.next(); qDebug() < < i.key() Read more →

Set QIcon or say Icon in QListView

Create a new model class based on QSqlQueryModel and implement data() method like below: QVariant SqlQueryModel::data ( const QModelIndex & item, int role) const { if (role == Qt::DecorationRole) { switch(item.column()) { case 0: // column 0 contains text data QPixmap image; // column 1 contains BLOB data image.loadFromData(record(item.row()).value(1).toByteArray()); return image.scaled(160, 160, Qt::KeepAspectRatio, Qt::SmoothTransformation); } if (value.isValid() && role ==… Read more →

Different Mouse Events in Qt

bool MyQMainWindowEventFilter::eventFilter (QObject* o, QEvent* e) { // play with all mouse events if ((e->type() == QEvent::MouseButtonPress) || (e->type() == QEvent::MouseButtonRelease) || (e->type() == QEvent::MouseButtonDblClick) || (e->type() == QEvent::MouseMove)) { cout < < "mouse event" Read more →

Removing MinGW dll dependencies in QT Static linking

libgcc_s_dw2-1.dll Passing -static-libgcc will remove this dependency for all languages other than C. Note: C++ exceptions depends on this option. mingwm10.dll Remove -mthreads option from your makefile. Note: Multithreading and C++ exceptions depends on this option. Read more →

Use QColor from getColor on QLabel and QTextbox

//To use color on text and label //Simple sample :) QColor color = QColorDialog::getColor(QColor(ui->backgroundColorPickerText->text()), this); if(color.isValid()) { QPixmap pix(21, 21); pix.fill(color); ui->backgroundColorPickerLabel->setPixmap(pix); ui->backgroundColorPickerText->setText(color.name()); } Read more →

create a custom dialog box with multi line input in qt

//create a custom dialog box with multi line input void MainWindow::createMultilineInput() { //class variable dlgMultiLine = new QDialog(this); //local variable QGridLayout *gridLayout = new QGridLayout(dlgMultiLine); //class variable txtMultiline = new QPlainTextEdit(); txtMultiline->setObjectName(QString::fromUtf8(“txtMultiline”)); gridLayout->addWidget(txtMultiline, 0, 0, 1, 1); QDialogButtonBox *buttonBox = new QDialogButtonBox(); buttonBox->setObjectName(QString::fromUtf8(“buttonBox”)); buttonBox->setOrientation(Qt::Horizontal); buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok); gridLayout->addWidget(buttonBox, 1, 0, 1, 1); connect(buttonBox,SIGNAL(accepted()),this,SLOT(on_acceptLoadHtml())); connect(buttonBox,SIGNAL(rejected()),this,SLOT(on_rejectLoadHtml())); dlgMultiLine->show(); } Read more →

show data in external window with custom widget in qt

Sometime you need to show data in external window with custom widget(s). This little code will help you. function showCustomWidget() { QDialog *dlgMultiLine = new QDialog(); QGridLayout *gridLayout = new QGridLayout(dlgMultiLine); QPlainTextEdit *txtMultiline = new QPlainTextEdit(); txtMultiline->setPlainText(this->toPlainText()); gridLayout->addWidget(txtMultiline, 0, 0, 1, 1); dlgMultiLine->show(); } Read more →

getting all files from subdirectories recursively with c using qt framework/

// Display a dialog to the user to choose his music directory QString directory_path = QFileDialog::getExistingDirectory(this, tr(“Select your music directory”), QDir::currentPath()); // Then create an instance of our QDirIterator, which takes as parameters // the directory, a QDir filter and an option flag which the QDirIterator is told // to go on the subdirectories also. // I have combined the… Read more →

Create single instance application in Qt 4 : Qt Tip #1

Many times we want only single instance of application. How can we achieve this. I’m going to show a simple way to achieve this. We need a Utility that is freely provided by Qt guys . Click here to know more qtsingleapplication Download zip version of this utility from here Extract file from zip into your project folder. Rename folder… Read more →

Simulate a Key event press/release in QT 4

QApplication::sendEvent(this, new QKeyEvent (QEvent::KeyPress, Qt::Key_Dead_Diaeresis, Qt::NoModifier, QString(“¨”))); //Another way I guess easy to understand :) //Although both are same QKeyEvent keyEvent(QEvent::KeyPress,Qt::Key_Escape, Qt::NoModifier); QApplication::sendEvent(this, &keyEvent); Read more →

Qt Code Example: New File | Open File | Save File | Save as File | File Print | Print Preview | File Print Preview | Print Preview

These are few code snippet taken from Qt examples. I’m making easier to look for particular functions. That’s it :) . New File Open File Save File Save as File File Print Print Preview File Print Preview Print Preview Save as PDF void TextEdit::fileNew() { if (maybeSave()) { textEdit->clear(); setCurrentFileName(QString()); } } void TextEdit::fileOpen() { QString fn = QFileDialog::getOpenFileName(this, tr(“Open… Read more →

Qt Code Example: MessageBox

QMessageBox::about(this, tr(“About Syntax Highlighter”), tr(“The Syntax Highlighter example shows how ” \ “to perform simple syntax highlighting by subclassing ” \ “the QSyntaxHighlighter class and describing ” \ “highlighting rules using regular expressions.”)); Read more →

Qt Code Example : Open File

void MainWindow::openFile(const QString &path) { QString fileName = path; if (fileName.isNull()) fileName = QFileDialog::getOpenFileName(this, tr(“Open File”), “”, “C++ Files (*.cpp *.h)”); if (!fileName.isEmpty()) { QFile file(fileName); if (file.open(QFile::ReadOnly | QFile::Text)) editor->setPlainText(file.readAll()); } } Read more →