Qt 4:如何在主窗口的菜单中插入QAction

时间:2014-03-24 08:51:52

标签: c++ qt menu widget

我的问题是从外部获取自定义小部件的QAction并插入主窗口的菜单中。有QActionWidget,但它在菜单中插入自定义小部件。

 class QDESIGNER_WIDGET_EXPORT Photo_list: public QWidget
{
  Q_OBJECT

public:
  Photo_list(QWidget* parent = 0);
  ~Photo_list();
  int count();
  QString returnImagePath(const int i);
  void sendSignalImageLoaded();

public slots:
  void addImagePath();
  void deleteImagePath();

signals:
  void imageLoaded(int count);

  //private:
public:
  Ui::Photo_list *m_photo_list_ui;
  QStringList m_image_path_list;
};



 class Ui_Photo_list
{
public:
    QAction *addImageAction;
    QAction *deleteImageAction;
    QGridLayout *gridLayout;
    QListWidget *listWidget;

    void setupUi(QWidget *Photo_list)
    {
        if (Photo_list->objectName().isEmpty())
            Photo_list->setObjectName(QString::fromUtf8("Photo_list"));
        Photo_list->resize(274, 210);
        addImageAction = new QAction(Photo_list);
        addImageAction->setObjectName(QString::fromUtf8("addImageAction"));
        deleteImageAction = new QAction(Photo_list);
        deleteImageAction->setObjectName(QString::fromUtf8("deleteImageAction"));
        gridLayout = new QGridLayout(Photo_list);
        gridLayout->setContentsMargins(0, 0, 0, 0);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        gridLayout->setHorizontalSpacing(0);
        listWidget = new QListWidget(Photo_list);
        listWidget->setObjectName(QString::fromUtf8("listWidget"));
        listWidget->setTextElideMode(Qt::ElideMiddle);
        listWidget->setMovement(QListView::Static);
        listWidget->setResizeMode(QListView::Adjust);
        listWidget->setViewMode(QListView::IconMode);

        gridLayout->addWidget(listWidget, 0, 0, 1, 1);


        retranslateUi(Photo_list);

        QMetaObject::connectSlotsByName(Photo_list);
    } ...

如何在(*)中添加QAction * addImageAction? (见下面的评论)

 class Ui_Cpdfa
{
public:
    QAction *createPdfAction;
    QWidget *centralwidget;
    QGridLayout *gridLayout_2;
    QGridLayout *gridLayout;
    QLineEdit *pdfPathLineEdit;
    QPushButton *findPdfPathButton;
    Photo_list *photo_list;
    QToolBar *toolBar;
    QMenuBar *menuBar;
    QMenu *menu;
    QMenu *menu_2;

    void setupUi(QMainWindow *Cpdfa)
    {
        if (Cpdfa->objectName().isEmpty())
            Cpdfa->setObjectName(QString::fromUtf8("Cpdfa"));
        Cpdfa->resize(386, 303);
        createPdfAction = new QAction(Cpdfa);
        createPdfAction->setObjectName(QString::fromUtf8("createPdfAction"));
        centralwidget = new QWidget(Cpdfa);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        gridLayout_2 = new QGridLayout(centralwidget);
        gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));
        gridLayout = new QGridLayout();
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        pdfPathLineEdit = new QLineEdit(centralwidget);
        pdfPathLineEdit->setObjectName(QString::fromUtf8("pdfPathLineEdit"));

        gridLayout->addWidget(pdfPathLineEdit, 0, 0, 1, 1);

        findPdfPathButton = new QPushButton(centralwidget);
        findPdfPathButton->setObjectName(QString::fromUtf8("findPdfPathButton"));

        gridLayout->addWidget(findPdfPathButton, 0, 1, 1, 1);

        photo_list = new Photo_list(centralwidget);
        photo_list->setObjectName(QString::fromUtf8("photo_list"));


        gridLayout->addWidget(photo_list, 1, 0, 1, 2);


        gridLayout_2->addLayout(gridLayout, 0, 0, 1, 1);

        Cpdfa->setCentralWidget(centralwidget);
        toolBar = new QToolBar(Cpdfa);
        toolBar->setObjectName(QString::fromUtf8("toolBar"));
        Cpdfa->addToolBar(Qt::TopToolBarArea, toolBar);
        menuBar = new QMenuBar(Cpdfa);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 386, 21));
        menu = new QMenu(menuBar);
        menu->setObjectName(QString::fromUtf8("menu"));
        menu_2 = new QMenu(menuBar);
        menu_2->setObjectName(QString::fromUtf8("menu_2"));
        Cpdfa->setMenuBar(menuBar);

        toolBar->addAction(createPdfAction); //!!(*)
        toolBar->addSeparator();
        menuBar->addAction(menu->menuAction());
        menuBar->addAction(menu_2->menuAction());
        menu->addSeparator();
        menu_2->addAction(createPdfAction); //!!(*)

        retranslateUi(Cpdfa);

        QMetaObject::connectSlotsByName(Cpdfa);
    } // setupUi

1 个答案:

答案 0 :(得分:0)

我猜你正在使用Qt创建者并使用表单编辑器单独设计表单。

您的问题是,您希望类A(包含子对象的“更大”窗口小部件)访问其子对象B(您的photo_list窗口小部件)的命名空间中的成员。

要做到这一点,

  1. 将包含"ui_B.h"B.cpp移至B.h

  2. B.h内部公开私人信息Ui::B *ui;(或者您可以AB成为朋友)

  3. 在包含"B.h"并拥有子对象指针*B之后,您可以在B->ui->actionXXX中致电A以访问B的所有成员, B->ui指向包含从B.ui文件创建的所有成员的名称空间(结果显示在"ui_B.h"内)