Skip to content

diegoiast/command-palette-widget

Repository files navigation

CommandPaletteWideget

This is a Qt widget that behaves like the command pallete found in modern applications (SublimeText, VSCode etc).

Command palette demo

Features:

  • You can feed it any QAbstractItemModel (tree based models are still WIP).
  • When the user chosess an item - a singal is emited.
  • Special filters (fuzzy search, remove accelerators, file search)
  • If your list contains list of files, you can set the filter to file based (searches for part of filenames/dirs etc).

Basic usage

Feeding it a list of strings:

auto commandPalette = new CommandPalette(this);
auto model = new QStringListModel({
    "All you need is love.",
    "We all want to change the world.",
    "Here comes the sun.",
    "I get by with a little help from my friends.",
    "With a little help from my friends.",
    "Let it be.",
    "Come together, right now.",
    "I want to hold your hand.",
    "Help! I need somebody.",
    "Imagine all the people living life in peace.",
    "You say goodbye and I say hello.",
    "Yesterday, all my troubles seemed so far away.",
    "I am the walrus.",
    "Lucy in the sky with diamonds.",
    "Hey Jude, don't make it bad."},
    this
);
commandPalette->setDataModel(model);
commandPalette->clearText();
commandPalette->show();
connect(commandPalette, &CommandPalette::didChooseItem, this,
        [model this](const QModelIndex index, const QAbstractItemModel *model) {
    QMessageBox msgBox;
    msgBox.setWindowTitle("Message");
    msgBox.setText(index.data().toString());
    msgBox.exec();
});

You can also feed it a the list of available commands, from your window. Note the function collectWidgetActions() which is in this library. This will search for all QActions in your window, from menus and toolbars.

auto commandPalette = new CommandPalette(this);
auto model = new ActionListModel(this);
model->setActions(collectWidgetActions(this));
commandPalette->setDataModel(model);
commandPalette->setFilterModes(CommandPalette::FilterMode::FileMatch);
connect(commandPalette, &CommandPalette::didChooseItem, this,
        [](const QModelIndex &index, const QAbstractItemModel *model) {
    auto data = model->data(index, Qt::UserRole);
    auto action = data.value<QAction *>();
    if (action) {
        action->trigger();
    }
});

Integration

You can use CPM - https://github.com/cpm-cmake/CPM.cmake

include(CPM.cmake) 
CPMAddPackage("gh:diegoiast/command-palette-widget#main") 

You can use standard CMake:

include(FetchContent)
FetchContent_Declare(
    command_palette_widget
    GIT_REPOSITORY https://github.com/diegoiast/command-palette-widget.git
    GIT_TAG main
)
FetchContent_MakeAvailable(command_palette_widget)

You can use a git subtree, and add that subdirectory:

add_subdirectory(libs/3rd-party/command-pallete-widget)

Alternative option: copy the following files to your project, and build as usual.

include/CommandPaletteWidget/CommandPalette
include/CommandPaletteWidget/commandpalette.h
src/commandpalette.cpp

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published