English 中文(简体)
我怎么能把QColorDialog用在另一个植被中,而不是作为一种单独的方言?
原标题:
  • 时间:2009-05-20 15:31:30
  •  标签:

我愿使用QColorDialog。 不是作为方言窗口,而是作为我可以插入布局的植被。 (具体来说,在菜单中作为习俗子菜单)

我研究了QColorDialog源码,我或许可以在内部实施QColorDialog的过程中复制,以达到这一目的,但是否有更清洁的方法这样做? 我使用了Qt 4.5.1。

最佳回答

如果有一种干净的方法做到这一点,我不知道。从我的角度来看,你有一些选择:

  • Subclass it and copy the code that actually constructs the widget, making edits to remove the part that creates the dialog window and replace it with some other container.
  • If you re not dead-set on using that particular dialog, the color triangle widget from qt solutions might work, because it isn t a dialog window. You can find it at http:// doc.trolltech.com/solutions/4/qtcolortriangle/qtcolortriangle.html (remove the space from the link)
问题回答

QColorDialog是一个方言,这意味着ITIS是一种植被。 你们都需要做的是树立几条窗口旗帜,并像你所希望的那样,把它放入你的布局。 例如:

#include <QApplication>
#include <QMainWindow>
#include <QColorDialog>

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

    /* setup a quick and dirty window */
    QMainWindow app;
    app.setGeometry(250, 250, 600, 400);

    QColorDialog *colorDialog = new QColorDialog(&app);
    /* set it as our widiget, you can add it to a layout or something */
    app.setCentralWidget(colorDialog);
    /* define it as a Qt::Widget (SubWindow would also work) instead of a dialog */
    colorDialog->setWindowFlags(Qt::Widget);
    /* a few options that we must set for it to work nicely */
    colorDialog->setOptions(
                /* do not use native dialog */
                QColorDialog::DontUseNativeDialog
                /* you don t need to set it, but if you don t set this
                    the "OK" and "Cancel" buttons will show up, I don t
                    think you d want that. */
                | QColorDialog::NoButtons
    );

    app.show();
    return a.exec();
}

你们可以通过设定正确的窗口旗帜,以非常简单的方式清理。

QColorDialog8 colorDialog = new ....
colorDialog->setWindowFlags(Qt::SubWindow);

你们也许想看一下某些Qt解决办法,至少会做你们想要的东西的一部分。 例如,见Color Picker Solutions,他们注意到,目前也可作为LGPL-许可的图书馆查阅。

作为一个替代方案(可能不太受支持的方法),我想起了Qt-Labs中一些关于将Qt小部件(包括QDialog)嵌入到QGraphicsScene中的工作。你可以潜在地这样做,然后改变你的图形场景的视图,以便只有你感兴趣的颜色选择器对话框部分对用户可见。听起来非常hackish,不过。

履历

http://www.ohchr.org。 如果你想要显示方言,则在植被上添加<代码>QGraphicsView。

在@Wiz的答复的基础上,我利用C++11的一些特征(lambdas和汽车;与VS2010和gcc4.6和Qt 5.1合作)将地雷放在了工具棒 but子之外:

auto dialog = new QColorDialog();
dialog->setWindowFlags( Qt::Widget );
dialog->setOptions( QColorDialog::DontUseNativeDialog | QColorDialog::ShowAlphaChannel );

auto action = new QWidgetAction( 0 );
action->setDefaultWidget( dialog );

auto menu = new QMenu();
menu->addAction( action );

// The dialog-as-widget closes on Ok/cancel, but the menu that holds it 
// doesn t. We connect the two here. Because the dialog hides itself,
// we need to reshow it when the menu is coming up again.
connect( menu,   &QMenu::aboutToShow,     [=] { dialog->show(); } );
connect( dialog, &QColorDialog::rejected, [=] { menu->hide();   } );
connect( dialog, &QColorDialog::colorSelected,
    [=]( const QColor& color )
    {
        menu->hide();
        OnFillColorChanged( color ); // Call the "slot" in this class
    });

auto button = new QToolButton();
button->setIcon( QIcon( ":/images/whatev.png") );
button->setText( tr("Fill") );
button->setStatusTip( tr("Choose fill color") );
button->setMenu( menu );
button->setPopupMode( QToolButton::InstantPopup );
button->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );

toolbar->addWidget( button ); // toolbar is defined elsewhere

根据先前“金属”的答复,即建议你在衍生的“QAction”类别中采用以下方法:

void MyQAction::setPopupDialog(QDialog* dialog) {

  QWidgetAction* action = new QWidgetAction(NULL);
  action->setDefaultWidget(dialog);

  QMenu* menu = new QMenu();
  menu->addAction(action);
  // Fix issues related to the way the dialogbox hide/show. Restablish proper handling,
  // based on our requirement.
  connect(menu, SIGNAL(aboutToShow()), dialog, SLOT(show()));
  connect(dialog, SIGNAL(finished(int)), menu, SLOT(hide()));

  setMenu(menu);
}

这将使任何方言箱的程序自动化。





相关问题
热门标签