Context menu für ein QTableWidget Dialog ?

Alles rund um die Programmierung mit Qt
Antworten
defenderLQ
Beiträge: 156
Registriert: 27. Juli 2006 20:53

Context menu für ein QTableWidget Dialog ?

Beitrag von defenderLQ »

hi

ich will nun eine context menu erstellen es klappt aber nciht!
ich kann im Dialog mit rechts klick nichts auswählen!


hier mein DownloadsDialog.cpp:

Code: Alles auswählen

#include "rshare.h"
#include "DownloadsDialog.h"
#include <QHeaderView>

#include <QContextMenuEvent>
#include <QMenu>
#include <QCursor>


/** Constructor */
DownloadsDialog::DownloadsDialog(QWidget *parent)
: MainPage(parent)
{
  /* Invoke the Qt Designer generated object setup routine */
  ui.setupUi(this);



  /* Hide platform specific features */
#ifdef Q_WS_WIN

#endif
}



void DownloadsDialog::contextMenuEvent(QContextMenuEvent*)
{
               
        popMenu = new QMenu(this);   
        popMenu->addAction(_showdowninfoAct);
        popMenu->addSeparator();
        popMenu->addAction(_resumeAct);
        popMenu->exec();  
        
}

void DownloadsDialog::createActions()
{


  _showdowninfoAct = new QAction(QIcon(), tr("Show Informations"), this);
  connect(_showdowninfoAct, SIGNAL(triggered()), this, SLOT(showPreferencesWindow()));
  
  _resumeAct = new QAction(QIcon(), tr("Resume"), this);
  connect(_resumeAct, SIGNAL(triggered()), this, SLOT(close()));


}
und mein DownloadsDialog.h:

Code: Alles auswählen

#ifndef _DOWNLOADSDIALOG_H
#define _DOWNLOADSDIALOG_H

#include <QFileDialog>

#include <config/rsharesettings.h>

#include "mainpage.h"
#include "ui_DownloadsDialog.h"



class DownloadsDialog : public MainPage 
{
  Q_OBJECT

public:
  /** Default Constructor */
  DownloadsDialog(QWidget *parent = 0);
  /** Default Destructor */


  
 

private slots:

  
private:
  /** Create the actions on the tray menu or menubar */
  void createActions();
  /** Create the context popup menu and it's submenus */
  void contextMenuEvent( QContextMenuEvent* );

  /** Define the popup menus for the Context menu */
  QMenu* popMenu;
  /** Defines the actions for the context menu */
  QAction* _showdowninfoAct;
  QAction* _resumeAct;

  /** Adds a new action to the toolbar. */
  void addAction(QAction *action, const char *slot = 0);

  /** Qt Designer generated object */
  Ui::DownloadsDialog ui;
};

#endif
DownloadsDialog Properity Editor :

Bild

DownloadtableWidget Properity Editor:

Bild
defenderLQ
Beiträge: 156
Registriert: 27. Juli 2006 20:53

Beitrag von defenderLQ »

ich bin schon dran seit mindestens 2 wochen um das problem zu lösen aber
hoffe bald das ich es gefunden habe....
Einmaliger
Beiträge: 48
Registriert: 22. Juli 2006 11:42

Beitrag von Einmaliger »

Seit 2 Wochen???

Hast du schonmal versucht, das Kontextmenü über einen MouseEvent aufzumachen:

Code: Alles auswählen

void DownloadsDialog::mousePressEvent(QMouseEvent *e)
{
    if( e->button() == Qt::RightButton )
    {
        QMenu popMenu = new QMenu(this);   
        popMenu->addAction(_showdowninfoAct);
        popMenu->addSeparator();
        popMenu->addAction(_resumeAct);
        popMenu->exec();
        delete popMenu;
    }
}
Das delete nicht vergessen, sonst hast du ein Speicherleck.
Anton
Beiträge: 9
Registriert: 27. Juli 2006 13:11
Wohnort: Asperg

Beitrag von Anton »

Wenn ich diesen Programmcode richtig interpretiere, dann müsste Qt::DefaultContextMenu statt Qt::CustomContextMenu als contextMenuPolicity gewählt werden. Denn das popMenu wird im contextMenuEvent() handler aufgebaut.

s. Doku contextMenuPolicy : Qt::ContextMenuPolicy

...The default value of this property is Qt::DefaultContextMenu, which means the contextMenuEvent() handler is called...

Was mir noch aufgefallen ist: Im contextMenuEvent() handler wird das Menu bei jedem Ereigneis neu zugewiesen, ohne das evtl. bestehende Menu, das im Objekt definiert ist, per delete() aufzuräumen.

Definiere das QMenu* popMenu; doch einfach innerhalb des contextMenuEvent() handlers. Dann wird's automatisch aufgeräumt.

Anton
Qt Desktop Edition - Plattform: Windows - Version: 4.3.0
defenderLQ
Beiträge: 156
Registriert: 27. Juli 2006 20:53

Beitrag von defenderLQ »

ok ich grad nur zu default geändert nun taucht das context menue
ganz oben links immer auf.

Bild


wenn ich änderen zu:

Code: Alles auswählen

void DownloadsDialog::mousePressEvent(QMouseEvent *e)
{
    if( e->button() == Qt::RightButton )
    {
        QMenu popMenu = new QMenu(this);   
        popMenu->addAction(_showdowninfoAct);
        popMenu->addSeparator();
        popMenu->addAction(_resumeAct);
        popMenu->exec();
        delete popMenu;
    }
}
kommen bei mir einige fehler raus...
gui\DownloadsDialog.cpp: In member function `virtual void DownloadsDialog::mouse
PressEvent(QMouseEvent*)':
gui\DownloadsDialog.cpp:54: error: conversion from `QMenu*' to non-scalar type `
QMenu' requested
gui\DownloadsDialog.cpp:55: error: base operand of `->' has non-pointer type `QM
enu'
gui\DownloadsDialog.cpp:56: error: base operand of `->' has non-pointer type `QM
enu'
gui\DownloadsDialog.cpp:57: error: base operand of `->' has non-pointer type `QM
enu'
gui\DownloadsDialog.cpp:58: error: base operand of `->' has non-pointer type `QM
enu'
gui\DownloadsDialog.cpp:59: error: type `class QMenu' argument given to `delete'
, expected pointer
mingw32-make[1]: *** [temp\obj\DownloadsDialog.o] Error 1
in meiner h. datei siehts os aus:

Code: Alles auswählen

private:
  /** Create the actions on the tray menu or menubar */
  void createActions();
  /** Create the context popup menu and it's submenus */
  void mousePressEvent(QMouseEvent *e);
und in DownloadsDialog.cpp habe ich die
#include <QMouseEvent>

nicht vergessen

das context menu weiß nun nicht das ins tablewidget rein muss oder?
Anton
Beiträge: 9
Registriert: 27. Juli 2006 13:11
Wohnort: Asperg

Beitrag von Anton »

nehme den mousePressEvent-Handler wieder raus und
korrigiere den contextMenuEvent-Handler wie folgt:

void DownloadsDialog::contextMenuEvent(QContextMenuEvent *e)
{
QMenu popMenu(this);

popMenu.addAction(_showdowninfoAct);
popMenu.addSeparator();
popMenu.addAction(_resumeAct);
popMenu.exec(e->globalPos());
}

Die Cursor-Position ( QPoint pos = e->globalPos(); ) ist absolut zu topLeft des Bildschirms. Damit erscheint das Kontext-Menü auch an der richtigen Stelle.

Anton
Qt Desktop Edition - Plattform: Windows - Version: 4.3.0
defenderLQ
Beiträge: 156
Registriert: 27. Juli 2006 20:53

Beitrag von defenderLQ »

danke nun klappts aber was komisch ist die actions die tauchen im context menu nicht auf?

obwohl sie schon definiert sind.

Bild

Code: Alles auswählen

void DownloadsDialog::contextMenuEvent(QContextMenuEvent *e)
{
	QMenu popMenu(this);

	popMenu.addAction(_showdowninfoAct);
	popMenu.addSeparator();
	popMenu.addAction(_resumeAct);
	popMenu.exec(e->globalPos());
} 

void DownloadsDialog::createActions()
{


  _showdowninfoAct = new QAction(QIcon(), tr("Show Informations"), this);
  connect(_showdowninfoAct, SIGNAL(triggered()), this, SLOT(showPreferencesWindow()));
  
  _resumeAct = new QAction(QIcon(), tr("Resume"), this);
  connect(_resumeAct, SIGNAL(triggered()), this, SLOT(close()));


}
die actions sind noch nicht fertig aber die texte müssten schon sichtbar sein oder?
Anton
Beiträge: 9
Registriert: 27. Juli 2006 13:11
Wohnort: Asperg

Beitrag von Anton »

der Unterschied zum Code aus den Trolltech-Examples:

Dein Code:
_showdowninfoAct = new QAction(QIcon(), tr("Show Informations"), this);

trolltech:
saveAsAct = new QAction(tr("Save &As..."), this);

Wenn Du kein Icon Verwendest, dann lass QIcon() im Aufruf new QAction(... einfach weg.

Anton
Qt Desktop Edition - Plattform: Windows - Version: 4.3.0
defenderLQ
Beiträge: 156
Registriert: 27. Juli 2006 20:53

Beitrag von defenderLQ »

ok

es kommt der gleiche effect raus...

Code: Alles auswählen

void DownloadsDialog::contextMenuEvent(QContextMenuEvent *e)
{
	QMenu popMenu(this);

	popMenu.addAction(_showdowninfoAct);
	popMenu.addSeparator();
	popMenu.addAction(_resumeAct);
	popMenu.exec(e->globalPos());
} 

void DownloadsDialog::createActions()
{


  _showdowninfoAct = new QAction(tr("Show Informations"), this);
  connect(_showdowninfoAct, SIGNAL(triggered()), this, SLOT(showPreferencesWindow()));
  
  _resumeAct = new QAction(tr("Resume"), this);
  connect(_resumeAct, SIGNAL(triggered()), this, SLOT(close()));


}
hier ist nochmal meine DownloadsDialog.h :

Code: Alles auswählen

#ifndef _DOWNLOADSDIALOG_H
#define _DOWNLOADSDIALOG_H

#include <QFileDialog>

#include <config/rsharesettings.h>

#include "mainpage.h"
#include "ui_DownloadsDialog.h"



class DownloadsDialog : public MainPage 
{
  Q_OBJECT

public:
  /** Default Constructor */
  DownloadsDialog(QWidget *parent = 0);
  /** Default Destructor */


  
 

private slots:

  
private:
  /** Create the actions on the tray menu or menubar */
  void createActions();
  /** Create the context popup menu and it's submenus */
  void contextMenuEvent( QContextMenuEvent *e);

  /** Define the popup menus for the Context menu */
  QMenu* popMenu;
  /** Defines the actions for the context menu */
  QAction* _showdowninfoAct;
  QAction* _resumeAct;

  /** Adds a new action to the toolbar. */
  void addAction(QAction *action, const char *slot = 0);

  /** Qt Designer generated object */
  Ui::DownloadsDialog ui;
};
ich habs auch so probiert also auch das gleiche ergebnis....

Code: Alles auswählen

void DownloadsDialog::contextMenuEvent(QContextMenuEvent *e)
{
	//QMenu popMenu(this);
	popMenu = new QMenu(this);  
	
	//popMenu.addAction(_showdowninfoAct);
	//popMenu.addSeparator();
	//popMenu.addAction(_resumeAct);
	//popMenu.exec(e->globalPos());
	
	popMenu->addAction(_showdowninfoAct);
	popMenu->addSeparator();
	popMenu->addAction(_resumeAct);
	popMenu->exec(e->globalPos());
} 
Anton
Beiträge: 9
Registriert: 27. Juli 2006 13:11
Wohnort: Asperg

Beitrag von Anton »

Code: Alles auswählen

private slots:
 
private:
  void createActions();
  void contextMenuEvent( QContextMenuEvent *e);

  QMenu* popMenu;
  QAction* _showdowninfoAct;
  QAction* _resumeAct; 

Code: Alles auswählen

void DownloadsDialog::createActions()
{
  _showdowninfoAct = new QAction(tr("Show Informations"), this);
  connect(_showdowninfoAct, SIGNAL(triggered()), this, SLOT(showPreferencesWindow()));
 
  _resumeAct = new QAction(tr("Resume"), this);
  connect(_resumeAct, SIGNAL(triggered()), this, SLOT(close()));
}
die actions sind noch nicht fertig aber die texte müssten schon sichtbar sein oder?
Das verstehe ich nicht. Sind die SLOTS noch nicht implementiert?
Qt Desktop Edition - Plattform: Windows - Version: 4.3.0
defenderLQ
Beiträge: 156
Registriert: 27. Juli 2006 20:53

Beitrag von defenderLQ »

nun habe ich ein slot implementiert aber mir zeigt das context menu immer noch nichts an !


Code: Alles auswählen

#include "rshare.h"
#include "DownloadsDialog.h"
#include "downinfo/DownInfoWindow.h"
#include <QHeaderView>

#include <QContextMenuEvent>
#include <QMenu>
#include <QCursor>

/* Images for context menu icons */
#define IMAGE_INFO       ":/images/informations_16x16.png"

/** Constructor */
DownloadsDialog::DownloadsDialog(QWidget *parent)
: MainPage(parent)
{
  /* Invoke the Qt Designer generated object setup routine */
  ui.setupUi(this);


  /* Hide platform specific features */
#ifdef Q_WS_WIN

#endif
}


void DownloadsDialog::contextMenuEvent(QContextMenuEvent *e)
{
	//QMenu popMenu(this);
	popMenu = new QMenu(this);  
	
	//popMenu.addAction(_showdowninfoAct);
	//popMenu.addSeparator();
	//popMenu.addAction(_resumeAct);
	//popMenu.exec(e->globalPos());
	
	popMenu->addAction(_showdowninfoAct);
	popMenu->addSeparator();
	//popMenu->addAction(_resumeAct);
	popMenu->exec(e->globalPos());
} 

void DownloadsDialog::createActions()
{

  _showdowninfoAct = new QAction(QIcon(IMAGE_INFO), tr("Show Informations"), this);
  connect(_showdowninfoAct, SIGNAL(triggered()), this, SLOT(showDownInfoWindow()));
  
//  _resumeAct = new QAction(QIcon(), tr("Resume"), this);
//  connect(_resumeAct, SIGNAL(triggered()), this, SLOT(close()));

}

/** Shows Downloads Informations */
void DownloadsDialog::showDownInfoWindow()
{
    DownInfoWindow *downinfoWindow = new DownInfoWindow();
    downinfoWindow->show();
}
und meine .h:

Code: Alles auswählen

#ifndef _DOWNLOADSDIALOG_H
#define _DOWNLOADSDIALOG_H

#include <QFileDialog>

#include <config/rsharesettings.h>

#include "mainpage.h"
#include "ui_DownloadsDialog.h"


class DownloadsDialog : public MainPage 
{
  Q_OBJECT

public:
  /** Default Constructor */
  DownloadsDialog(QWidget *parent = 0);
  /** Default Destructor */

private slots:

  void showDownInfoWindow();
  
private:

  void createActions();

  void contextMenuEvent( QContextMenuEvent *e);


  QMenu* popMenu;

  QAction* _showdowninfoAct;
  QAction* _resumeAct;

  void addAction(QAction *action, const char *slot = 0);


  Ui::DownloadsDialog ui;
};

#endif
defenderLQ
Beiträge: 156
Registriert: 27. Juli 2006 20:53

Beitrag von defenderLQ »

ist jemand schon sowas passiert?
Antworten