Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Spanish
  4. Menu contextual (Context Menu)
Forum Updated to NodeBB v4.3 + New Features

Menu contextual (Context Menu)

Scheduled Pinned Locked Moved Spanish
2 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yunier0525
    wrote on last edited by
    #1

    Estoy tradando de usar un menú contextual personalizado según el elemento en un QTreeWidget. Pero no logro conectar una señal que me daría la salución (pienso yo).

    El problema está en que cuando ejecuto el programa, me da el siguiente error/warning
    Object::connect: No such slot QTreeWidget::contextMenu(QPoint)

    Aqui dejo mi código fuente, para ver si alguno de ustedes me puede ayudar.

    @#ifndef CUSTOMTREEWIDGET_H
    #define CUSTOMTREEWIDGET_H

    #include <QTreeWidget>

    class CustomTreeWidget : public QTreeWidget
    {
    public:
    CustomTreeWidget(QWidget *parent = 0);
    ~CustomTreeWidget();

    public slots:
    void contextMenu(const QPoint &pos);
    };

    #endif // CUSTOMTREEWIDGET_H

    #include "customtreewidget.h"
    #include "defaulttreeitem.h"

    /constructor aqui/

    void CustomTreeWidget::contextMenu(const QPoint &pos)
    {
    DefaultTreeItem* selected =
    reinterpret_cast<DefaultTreeItem*>(currentItem());

    if ( selected != 0 )
        selected->contextMenu(pos);
    

    }

    #ifndef DEFAULTTREEITEM_H
    #define DEFAULTTREEITEM_H

    #include <QTreeWidgetItem>

    class DefaultTreeItem : public QTreeWidgetItem
    {
    public:
    explicit DefaultTreeItem(QTreeWidget *view, int type = Type);
    explicit DefaultTreeItem(QTreeWidgetItem *parent, int type = Type);
    ~DefaultTreeItem();

    void contextMenu(const QPoint &pos);
    

    };

    #endif // DEFAULTTREEITEM_H

    #include "defaulttreeitem.h"
    #include <QMenu>
    #include <QAction>
    /constructor aqui/

    void DefaultTreeItem::contextMenu(const QPoint &pos)
    {
    // creando el menú contextual
    QMenu* contextMenu = new QMenu();
    QAction *actCustom = new QAction("Custom Action", contextMenu);
    contextMenu->addAction(actCustom);
    contextMenu->exec( pos );
    delete contextMenu;
    }

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <customtreewidget.h>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;
    CustomTreeWidget * tree;

    };

    #endif // MAINWINDOW_H

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    this->tree = new CustomTreeWidget(ui->centralWidget);
    this->tree->setContextMenuPolicy(Qt::CustomContextMenu);
    
    connect(this->tree,SIGNAL ( customContextMenuRequested(QPoint) ),
            this->tree,SLOT ( contextMenu(QPoint) ) );
    

    }

    /....codigo aqui..../

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

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Necessita usar el macro Q_OBJECT en su customtreewidget.h.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved