Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Qt QAction: Example 1

 

QAction: Example 1 is a QAction example that DOES NOT WORK. I don't know why yet...

 

 

 

 

 

 

 

 

 

 

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: CppQActionExample1.pro

 

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TEMPLATE = app
SOURCES += main.cpp \
        qtmainwindow.cpp

HEADERS  += qtmainwindow.h
FORMS    += qtmainwindow.ui

 

 

 

 

 

main.cpp

 

#include "qtmainwindow.h"
#include <QApplication>

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

 

 

 

 

 

qtmainwindow.h

 

#ifndef QTMAINWINDOW_H
#define QTMAINWINDOW_H

#include <QMainWindow>

namespace Ui {
  class QtMainWindow;
}

class QtMainWindow : public QMainWindow
{
  Q_OBJECT
  
public:
  explicit QtMainWindow(QWidget *parent = 0);
  ~QtMainWindow();
  
private:
  Ui::QtMainWindow *ui;

private slots:
  void OnNew();
};

#endif // QTMAINWINDOW_H

 

 

 

 

 

qtmainwindow.cpp

 

#include "qtmainwindow.h"
#include "ui_qtmainwindow.h"

QtMainWindow::QtMainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::QtMainWindow)
{
  ui->setupUi(this);
  {
    //DOES NOT WORK???
    QAction * const a = new QAction(this);
    a->setShortcut(Qt::CTRL + Qt::Key_N);
    QObject::connect(a,SIGNAL(triggered()),this,SLOT(OnNew()));
    this->menuBar()->addAction("New",a,SLOT(trigger()));
  }
  {
    //DOES NOT WORK???
    QAction * const a = new QAction(this);
    a->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
    QObject::connect(a,SIGNAL(triggered()),this,SLOT(close()));
    this->menuBar()->addAction("Quit",a,SLOT(trigger()));
  }
}

QtMainWindow::~QtMainWindow()
{
  delete ui;
}

void QtMainWindow::OnNew()
{
  ui->label_random->setText(QString::number(std::rand()));
}

 

 

 

 

 

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