Qt QAccel question (very basic)

H

Hajo Molegraaf

Hi,

I'm doing my first steps in Qt programming and there is something that
I don't know how to do. As an excercise I want to write some
filemanager like application and I'm trying to implement copy/paste
features. I have two classes derived from QListView that give me two
views. I want the application to respond to the key CTRL-C or menu
option 'copy' by calling the corresponding 'action_copy' function from
one of the two classes (see below). I probably have to use QAccel but
I don't know how.

What follows is some small source code that has the basics of the
problem. What I want is that if I press CTRL-C in the left view
(belonging to class ViewA), the function ViewA::action_copy is called
and mutatis mutandis for the right view.

Thanks,
Hajo.



#include <qmainwindow.h>
#include <qlistview.h>
#include <qapplication.h>
#include <qsplitter.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qmessagebox.h>

class ViewA : public QListView
{
Q_OBJECT
public:
ViewA( QWidget* parent, const char* name = 0 );
// other function specific to ViewA
public slots:
void action_copy();
};

class ViewB : public QListView
{
Q_OBJECT
public:
ViewB( QWidget* parent, const char* name = 0 );
// other function specific to ViewB
public slots:
void action_copy();
};

class App : public QMainWindow
{
Q_OBJECT
public:
App();
public slots:
void menu_copy();
protected:
ViewA* viewA;
ViewB* viewB;
};

ViewA::ViewA( QWidget* parent, const char* name ) :
QListView(parent,name)
{
addColumn("column");
// something here probably
}

void ViewA::action_copy()
{
QMessageBox::information( this, "ViewA", "ViewA::menu_copy()" );
}

ViewB::ViewB( QWidget* parent, const char* name ) :
QListView(parent,name)
{
addColumn("column");
// something here probably
}

void ViewB::action_copy()
{
QMessageBox::information( this, "ViewB", "ViewB::menu_copy()" );
}

App::App()
{
QPopupMenu *edit = new QPopupMenu( this );
edit->insertItem( "&Copy", this, SLOT(menu_copy()), CTRL+Key_C );
QMenuBar* menu = menuBar();
menu->insertItem( "&Edit", edit );

QSplitter* splitter = new QSplitter( Qt::Horizontal, this );
viewA = new ViewA(splitter);
viewB = new ViewB(splitter);

setCentralWidget(splitter);
}

void App::menu_copy()
{
QMessageBox::information( this, "App", "App::menu_copy()" );
}

int main( int argc, char **argv )
{
QApplication a( argc, argv );

App app;
app.resize( 640, 480 );
a.setMainWidget( &app );
app.show();

return a.exec();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top