【发布时间】:2022-01-04 23:55:30
【问题描述】:
我的部分界面中有一个列表框,我想按进度单独突出显示 GtkListBoxRows。您加载了几个文件,我的程序单独处理每个文件,我想像进度条一样突出显示列表框行。它很像一个进度条,只是里面的内容是按钮和一些文字。是否有特定的 Cairo/Pango 函数允许重新着色?
【问题讨论】:
标签: gtk background-color
我的部分界面中有一个列表框,我想按进度单独突出显示 GtkListBoxRows。您加载了几个文件,我的程序单独处理每个文件,我想像进度条一样突出显示列表框行。它很像一个进度条,只是里面的内容是按钮和一些文字。是否有特定的 Cairo/Pango 函数允许重新着色?
【问题讨论】:
标签: gtk background-color
我在这里有一个使用 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`
【讨论】:
GtkListBoxRow 小部件的解决方案