在 GTK+ 中部分突出顯示小部件

ADBeveridge

我的界面中有一個列錶框,我想按進度單獨突出顯示 GtkListBoxRows。您加載了多個文件,我的程序會單獨處理每個文件,我想像進度條一樣突出顯示列錶框行。它非常類似於進度條,只是裡面的內容是按鈕和一些文字。是否有允許重新著色的特定 Cairo/Pango 功能?

鮑勃·莫蘭

我在這裡有一個使用 Gtkmm 的解決方案(應該很容易翻譯成 C)。我有一系列 5 個按鈕在容器內水平對齊,還有一個“取得進展”按鈕。單擊它時,容器中的子按鈕會更新以顯示進度:

#include <iostream>
#include <gtkmm.h>

class MainWindow : public Gtk::ApplicationWindow
{

public:

    MainWindow();

private:

    Gtk::Grid m_container;
    Gtk::Button m_progressButton;

    int m_progressTracker = 0;
};

MainWindow::MainWindow()
: m_progressButton("Make progress...")
{
    // Add five buttons to the container (horizontally):
    for(int index = 0; index < 5; ++index)
    {
        Gtk::Button* button = Gtk::make_managed<Gtk::Button>("B" + std::to_string(index));
        m_container.attach(*button, index, 0, 1, 1);
    }

    // Add a button to control progress:
    m_container.attach(m_progressButton, 0, 1, 5, 1);

    // Add handler to the progress button.
    m_progressButton.signal_clicked().connect(
        // Each time the button is clicked, the "hilighting" of the buttons
        // in the container progresses until completed:
        [this]()
        {
            Gtk::Widget* child = m_container.get_child_at(m_progressTracker, 0);
            if(child != nullptr)
            {
                std::cout << "Making progress ..." << std::endl;

                // Change the button's background color:
                Glib::RefPtr<Gtk::CssProvider> cssProvider = Gtk::CssProvider::create();
                cssProvider->load_from_data("button {background-image: image(cyan);}");
                child->get_style_context()->add_provider(cssProvider, GTK_STYLE_PROVIDER_PRIORITY_USER);
    
                // Update for next child...
                ++m_progressTracker;
            }
        }
    );

    // Make m_container a child of the window:
    add(m_container);
}

int main(int argc, char *argv[])
{
    std::cout << "Gtkmm version : " << gtk_get_major_version() << "."
                                    << gtk_get_minor_version() << "."
                                    << gtk_get_micro_version() << std::endl;

    auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
  
    MainWindow window;
    window.show_all();
  
    return app->run(window);
}

在您的情況下,您將不得不調整容器和信號(也許您需要其他東西來觸發重繪),但就更改背景顏色而言,它的工作方式應該幾乎相同。您可以使用以下命令構建它(使用 Gtkmm 3.24):

g++ main.cpp -o example.out `pkg-config --cflags --libs gtkmm-3.0`

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章