Showing posts with label QT. Show all posts
Showing posts with label QT. Show all posts

2008-01-31

QT右键菜单

  1. 右键弹出菜单:
    重写void QWidget::contextMenuEvent(QContextMenuEvent)函数
    如:
    void MainWindow::contextMenuEvent( QContextMenuEvent* e)
    {
      QMenu *menu = new QMenu();
      menu->addAction(openAct);
      menu->addSeparator();
      menu->addAction(quitAct);
      menu->exec(e->globalPos());
      delete menu;
    }

  2. 定制原生器件的弹出菜单(某些器件可能本身就自带popMenu)
    那么如何获得已有的菜单项呢?利用:
    QList<QAction *> QWidget::actions() const
    返回的是其所包含QAction的一个列表。

    如:
    TextViewer::TextViewer() : QTextEdit()
    {
      this->setReadOnly(true);
    }

    void TextViewer::contextMenuEvent ( QContextMenuEvent* e )
    {
      QMenu *menu = createStandardContextMenu();

      // remove the separator between 'copy' & 'select all'
      menu->removeAction(menu->actions().at(1));

      // insert separator before 'copy'
      QAction *sepTop = menu->insertSeparator(menu->actions().at(0));
      // insert 'open' & 'top' before that separator
      menu->insertAction(sepTop, ((MainWindow*)this->parentWidget())->openAction());
      menu->insertAction(sepTop, ((MainWindow*)this->parentWidget())->topmostAction());

      // add separator after 'select all'
      menu->addSeparator();
      // add 'quit' after that separator
      menu->addAction(((MainWindow*)this->parentWidget())->quitAction());

      menu->exec(e->globalPos());

      delete menu;
    }

    效果预览:



//EOF Read More...

QT快捷键

  1. shortcut作用域。
        QT 无全局快捷键(全局快捷键与相应的窗口管理器(KDE等)相关)。
    QAction::setShortcutContext(Qt::ShortcutContext)
      Qt::ApplicationShortcut 当前程序中有任意一窗口被激活时可使用的快捷键
      Qt::WindowShortcut      仅适用于当前窗口的 (default)
      Qt::WidgetShortcut      当前器件被激活时可用的快捷键


  2. 若该窗口无menuBar,对于popupMenu[contextMenu]中的menuItem之快捷键会无效。

      QAction 为抽象器件,可将该QAction添加到该窗口中,则可使用该菜单项的快捷键了 。


//EOF Read More...

QT程序外观定制

今天才发现,原来QT的程序可以像网页写CSS那样来用StyleSheet定制外观,真是方便啊。
(Qt >= 4.2) 其语法、作用域、优先级和网页的CSS差不多。
QApplication::setStyleSheet(QString);
QWidget::setStyleSheet(QString);

  1. 程序级的外观:(作用于整个程序的器件)
    如:
    QLineEdit { background: yellow }
    QCheckBox { color: red }

  2. 器件级的外观:(作用于本器件及其子器件)
    如:
    textViewer->
      setStyleSheet("background-color: #FFFFBB;"
                    "color: #000099;"
                    "margin: 10px;"
                    "padding: 5px;"
                    "padding-left: 15px;"
                    "padding-right: 15px;"
                    "border-radius: 5px;" // 圆角边框(只用IE的人就不知道了吧:D)
                    "border: 3px solid #abc;"
    );

    效果预览:




//EOF Read More...