PDA

View Full Version : Context menu



dejvis
19th September 2009, 19:09
Hello, i know there is alot post about context menu but i still can't handle it...

so: iam trying to make context menu but only in treeview and columnview, was sitting, reading and trying for 3hours now and i still can't manage to do it, any help?


#include "mainwindow.h"
#include <QtGui>

MainWindow::MainWindow ( QWidget* parent )
: QMainWindow ( parent )
{
setupUi ( this );


dirModel = new QDirModel(QStringList(), QDir::AllEntries |QDir::NoDotAndDotDot , QDir::Name | QDir::IgnoreCase);
dirModel->setReadOnly(true);

treeView1->setModel(dirModel);
treeView1->setAcceptDrops(true);
treeView1->setDragEnabled(true);


columnView1->setModel(dirModel);
columnView1->setAcceptDrops(true);
columnView1->setDragEnabled(true);

treeView2->setModel(dirModel);
treeView2->setAcceptDrops(true);
treeView2->setDragEnabled(true);

columnView2->setModel(dirModel);
columnView2->setAcceptDrops(true);
columnView2->setDragEnabled(true);

connect(actionDrzewko, SIGNAL(triggered()), this, SLOT(slotPage1()));
connect(actionKolumny, SIGNAL(triggered()), this, SLOT(slotPage2()));
connect(actionDrzewko_2, SIGNAL(triggered()), this, SLOT(slotPage3()));
connect(actionKolumny_2, SIGNAL(triggered()), this, SLOT(slotPage4()));
}


MainWindow::~MainWindow()
{}



void MainWindow::slotPage1()
{
stackedWidget->setCurrentIndex(0);
stacked1comboBox->setCurrentIndex(0);
}

void MainWindow::slotPage2()
{
stackedWidget->setCurrentIndex(1);
stacked1comboBox->setCurrentIndex(1);
}

void MainWindow::slotPage3()
{
stackedWidget_2->setCurrentIndex(0);
stacked2comboBox->setCurrentIndex(0);
}

void MainWindow::slotPage4()
{
stackedWidget_2->setCurrentIndex(1);
stacked2comboBox->setCurrentIndex(1);
}

void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);

menu.addAction(actionNowy);
menu.addAction(actionKopiuj);
menu.addAction(actionWytnij);
menu.addAction(actionWklej);
menu.exec(event->globalPos());
}



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include "ui_QTManagerUI.h"

class QDirModel;


class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT

public:
MainWindow ( QWidget* parent = 0 );
~MainWindow();

private:
QDirModel *dirModel;


private slots:
void slotPage1();
void slotPage2();
void slotPage3();
void slotPage4();

protected:
void contextMenuEvent(QContextMenuEvent *event);

};


#endif


edit: expamle of making new file, copying, cutting and pasting would be nice too

Lykurg
19th September 2009, 23:01
so: iam trying to make context menu but only in treeview and columnview, was sitting, reading and trying for 3hours now and i still can't manage to do it, any help?


Then you might use the contextMenuEvent method of your views and not of your mainwindow! Or check via the global position of the cursor, if you are over the views. But using the event handler of the classes is better. Or you could install an event filter...

dejvis
20th September 2009, 23:02
well thank you for your replay, gonna check if i can do something with it tommorow