Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QtExample13

 

QtQt CreatorLubuntuUbuntu

 

This Qt example shows how to create a more complete custom QDialog with a custom slot, like this screenshot (png). This example elaborates on Qt example 12: creating a custom QDialog with slot.

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQtExample13/CppQtExample13.pro

 

exists(../../DesktopApplication.pri) {
  include(../../DesktopApplication.pri)
}
!exists(../../DesktopApplication.pri) {
  QT += core gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  TEMPLATE = app

  CONFIG(debug, debug|release) {
    message(Debug mode)
  }

  CONFIG(release, debug|release) {
    message(Release mode)
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }

  QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra

  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

exists(../../Libraries/Boost.pri) {
  include(../../Libraries/Boost.pri)
}
!exists(../../Libraries/Boost.pri) {
  win32 {
    INCLUDEPATH += \
      ../../../Projects/Libraries/boost_1_55_0
  }
}

SOURCES += main.cpp

SOURCES  += mydialog.cpp
HEADERS  += mydialog.h

 

 

 

 

 

./CppQtExample13/main.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QApplication>
#include "mydialog.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  MyDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

./CppQtExample13/mydialog.h

 

#ifndef MYDIALOG_H
#define MYDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/scoped_ptr.hpp>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
#pragma GCC diagnostic pop

class MyDialog : public QDialog
{
  Q_OBJECT

public:

  MyDialog()
    : m_layout(new QVBoxLayout),
    m_button1(new QPushButton),
    m_button2(new QPushButton),
    m_clicks(0)
  {
    m_button1->setText("Count");
    m_button1->connect(
        m_button1.get(),SIGNAL(clicked()),
        this,SLOT(buttonClicked()));
    m_button2->setText("Quit");
    m_button2->connect(
        m_button2.get(),SIGNAL(clicked()),
        this,SLOT(close()));
    m_layout->addWidget(m_button1.get());
    m_layout->addWidget(m_button2.get());

    this->setGeometry(0,0,300,100);
    this->setWindowTitle("CppQtExample13");
    this->setLayout(m_layout.get());
  }

private:
  boost::scoped_ptr<QVBoxLayout> m_layout;
  boost::scoped_ptr<QPushButton> m_button1;
  boost::scoped_ptr<QPushButton> m_button2;
  int m_clicks;

private slots:
  void buttonClicked()
  {
    ++m_clicks;
    QString s; s = s.number(m_clicks);
    this->setWindowTitle("Top button is clicked " + s + " times");
  }
};

#endif // MYDIALOG_H

 

 

 

 

 

./CppQtExample13/mydialog.cpp

 

#include "mydialog.h"

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml