GtkTreeView 交替行颜色
Posted
技术标签:
【中文标题】GtkTreeView 交替行颜色【英文标题】:GtkTreeView alternate row color 【发布时间】:2020-02-06 08:05:06 【问题描述】:我无法设置 TreeView 的备用行颜色,我不知道为什么不起作用。
这是代码
MainWindow::MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade) :
Gtk::ApplicationWindow(cobject), refBuilder(refGlade)
Glib::RefPtr<Gtk::CssProvider> cssProvider = Gtk::CssProvider::create();
cssProvider->load_from_path("style.css");
Glib::RefPtr<Gtk::StyleContext> styleContext = Gtk::StyleContext::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
styleContext->add_provider_for_screen(screen, cssProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
const char* user = "user";
const char* passwd = "passwd";
const char* database = "dbname";
const char* dbhost = "192.168.1.5";
connection = mysql_init(NULL);
mysql_real_connect(connection, dbhost, user, passwd, database, 3307, NULL, 0);
refBuilder->get_widget("m_TreeView1", m_TreeView1);
m_refTreeModel = Gtk::ListStore::create(m_Columns);
m_TreeView1->set_model(m_refTreeModel);
/*Glib::RefPtr<Gtk::StyleContext> context = m_TreeView1->get_style_context();
context->add_class("treeview");
*/
Affitto* affitti = new Affitto(connection);
MYSQL_RES* res = affitti->getAll();
MYSQL_ROW riga;
Gtk::TreeModel::Row row;
while ((riga = mysql_fetch_row(res)))
row = *(m_refTreeModel->append());
row[m_Columns.m_col_inqid] = std::stoi(riga[0]);
row[m_Columns.m_col_cognomenome] = riga[1];
row[m_Columns.m_col_stabile] = riga[2];
row[m_Columns.m_col_mese] = std::stoi(riga[3]);
row[m_Columns.m_col_anno] = std::stoi(riga[4]);
row[m_Columns.m_col_posteggiopagato] = std::stod(riga[5]);
row[m_Columns.m_col_affittopagato] = std::stod(riga[6]);
row[m_Columns.m_col_riscaldamentopagato] = std::stod(riga[7]);
row[m_Columns.m_col_pagatoil] = riga[8];
row[m_Columns.m_col_stato] = riga[9];
row[m_Columns.m_col_osservazioni] = riga[10];
row[m_Columns.m_col_traslocato] = riga[11];
row[m_Columns.m_col_diversi] = std::stod(riga[12]);
row[m_Columns.m_col_affitto] = std::stod(riga[13]);
row[m_Columns.m_col_posteggio] = std::stod(riga[14]);
row[m_Columns.m_col_riscaldamento] = std::stod(riga[15]);
m_TreeView1->append_column("InqID", m_Columns.m_col_inqid);
m_TreeView1->append_column("Cognome e nome", m_Columns.m_col_cognomenome);
//m_TreeView1->get_column(1)->set_expand(true);
m_TreeView1->append_column("Stabile", m_Columns.m_col_stabile);
m_TreeView1->append_column("Mese", m_Columns.m_col_mese);
m_TreeView1->append_column("Anno", m_Columns.m_col_anno);
m_TreeView1->append_column("Posteggio pagato", m_Columns.m_col_posteggiopagato);
m_TreeView1->append_column("Affitto pagato", m_Columns.m_col_affittopagato);
m_TreeView1->append_column("Riscaldamento pagato", m_Columns.m_col_riscaldamentopagato);
m_TreeView1->append_column("Pagato il", m_Columns.m_col_pagatoil);
m_TreeView1->append_column("Stato", m_Columns.m_col_stato);
m_TreeView1->append_column("Osservazioni", m_Columns.m_col_osservazioni);
m_TreeView1->append_column("Traslocato", m_Columns.m_col_traslocato);
m_TreeView1->append_column("Diversi", m_Columns.m_col_diversi);
m_TreeView1->append_column("Affitto", m_Columns.m_col_affitto);
m_TreeView1->append_column("Posteggio", m_Columns.m_col_posteggio);
m_TreeView1->append_column("Riscaldamento", m_Columns.m_col_riscaldamento);
mysql_free_result(res);
mysql_close(connection);
这是正确加载的css。我在glade文件中设置了TreeView(treeview)的样式类。
.treeview row:nth-child(even)
background-color: #ff0000;
.treeview row:nth-child(odd)
background-color: #000000;
怎么了?我也尝试通过代码(注释行)设置样式类,但没有改变。 提前感谢您的帮助!
更新
我把我更新的代码,我错了什么?
Gtk::CellRendererText* textRenderer = manage(new Gtk::CellRendererText());
textRenderer->property_editable() = false;
Gtk::TreeViewColumn *col = manage(new Gtk::TreeViewColumn("InqId", *textRenderer));
col->add_attribute(*textRenderer, "cell-background-rgba", m_Columns.m_col_inqid);
m_TreeView1->append_column(*col);
while ((riga = mysql_fetch_row(res)))
row = *(m_refTreeModel->append());
row[m_Columns.m_col_inqid] = std::stoi(riga[0]);
row[m_Columns.m_col_inqid] = Gdk::Color("#CCCCCC");
提前致谢!
【问题讨论】:
这可能是一个错误? .treeview .row:nth-child(even) _ 我认为你在行之前错过了一个点(.),因为默认情况下 css 不承认一行是一个元素。 感谢您的回复,但它不起作用 您能否提供在浏览器中加载的该视图的 html 结构,以帮助我们找到解决方案。 这不是一个 html 页面...它是一个 gtk 应用程序。什么意思? 【参考方案1】:如果你想设置一行的背景颜色,你可以使用
cell-background-gdk
您可以阅读更多关于它的信息here
【讨论】:
谢谢,但我不明白如何使用它。我可以设置列的背景颜色,而不是整行的背景颜色。你能给我举个例子吗?提前非常感谢【参考方案2】:其他人已经遇到了同样的问题:
也许你可以做一个你抓住一行的每个单元格并使用的函数 已经提到的功能或在单元格创建时执行此操作:
Row Background Color GtkTreeView Widget
小心,他只是用错了颜色函数:
谢谢@ivo s,我使用了错误的属性,我需要 “cell-background-gdk”而不是“background-gdk”。感谢您的帮助。
使用 cell-background-rgba 函数代替前面提到的 cell-background-gdk 函数,因为 它已被弃用,并且将很快被删除或稍后。
【讨论】:
我已经看到了这个讨论......但我无法复制它......我现在更新我的第一篇文章我正在做的事情,如果你能帮助我看看我在做什么错了,非常感谢你【参考方案3】:所以,现在它可以工作了……这是完整的代码,以防有人遇到同样的问题。 非常感谢那些帮助我的人!
型号:
class MainWindow : public Gtk::ApplicationWindow
public:
MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade);
virtual ~MainWindow();
MYSQL* connection;
protected:
Glib::RefPtr<Gtk::Builder> refBuilder;
class ModelColumns : public Gtk::TreeModel::ColumnRecord
public:
ModelColumns()
add(m_col_inqid);
add(m_col_cognomenome);
add(m_col_stabile);
add(m_col_mese);
add(m_col_anno);
add(m_col_posteggiopagato);
add(m_col_affittopagato);
add(m_col_riscaldamentopagato);
add(m_col_pagatoil);
add(m_col_stato);
add(m_col_osservazioni);
add(m_col_traslocato);
add(m_col_diversi);
add(m_col_affitto);
add(m_col_posteggio);
add(m_col_riscaldamento);
add(m_coloreriga);
Gtk::TreeModelColumn<int> m_col_inqid;
Gtk::TreeModelColumn<std::string> m_col_cognomenome;
Gtk::TreeModelColumn<std::string> m_col_stabile;
Gtk::TreeModelColumn<int> m_col_mese;
Gtk::TreeModelColumn<int> m_col_anno;
Gtk::TreeModelColumn<double> m_col_posteggiopagato;
Gtk::TreeModelColumn<double> m_col_affittopagato;
Gtk::TreeModelColumn<double> m_col_riscaldamentopagato;
Gtk::TreeModelColumn<std::string> m_col_pagatoil;
Gtk::TreeModelColumn<std::string> m_col_stato;
Gtk::TreeModelColumn<std::string> m_col_osservazioni;
Gtk::TreeModelColumn<std::string> m_col_traslocato;
Gtk::TreeModelColumn<double> m_col_diversi;
Gtk::TreeModelColumn<double> m_col_affitto;
Gtk::TreeModelColumn<double> m_col_posteggio;
Gtk::TreeModelColumn<double> m_col_riscaldamento;
Gtk::TreeModelColumn<Gdk::RGBA> m_coloreriga;
;
ModelColumns m_Columns;
Gtk::TreeView* m_TreeView1;
Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
;
主要:
MainWindow::MainWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade) :
Gtk::ApplicationWindow(cobject), refBuilder(refGlade)
Glib::RefPtr<Gtk::CssProvider> cssProvider = Gtk::CssProvider::create();
cssProvider->load_from_path("style.css");
//cssProvider->load_from_data(cssProvider, ".treeview row:nth-child(even) background-color: #ff0000; .treeview row:nth-child(odd) background-color: #000000; ", -1, NULL);
Glib::RefPtr<Gtk::StyleContext> styleContext = Gtk::StyleContext::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
styleContext->add_provider_for_screen(screen, cssProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
const char* user = "root";
const char* passwd = "";
const char* database = "";
const char* dbhost = "192.168.1.5";
connection = mysql_init(NULL);
mysql_real_connect(connection, dbhost, user, passwd, database, 3307, NULL, 0);
refBuilder->get_widget("m_TreeView1", m_TreeView1);
//m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this, &MainWindow::on_button_quit) );
m_refTreeModel = Gtk::ListStore::create(m_Columns);
m_TreeView1->set_model(m_refTreeModel);
Affitto* affitti = new Affitto(connection);
MYSQL_RES* res = affitti->getAll();
MYSQL_ROW riga;
Gtk::TreeModel::Row row;
Gtk::CellRendererText* textRenderer = manage(new Gtk::CellRendererText());
Gtk::TreeViewColumn *col = manage(new Gtk::TreeViewColumn("InqId", *textRenderer));
col->add_attribute(*textRenderer, "cell-background-rgba", m_Columns.m_coloreriga);
m_TreeView1->append_column(*col);
textRenderer = manage(new Gtk::CellRendererText());
col = manage(new Gtk::TreeViewColumn("Cognome e nome", *textRenderer));
col->add_attribute(*textRenderer, "cell-background-rgba", m_Columns.m_coloreriga);
m_TreeView1->append_column(*col);
m_TreeView1->append_column("Stabile", m_Columns.m_col_stabile);
m_TreeView1->append_column("Mese", m_Columns.m_col_mese);
m_TreeView1->append_column("Anno", m_Columns.m_col_anno);
m_TreeView1->append_column("Posteggio pagato", m_Columns.m_col_posteggiopagato);
m_TreeView1->append_column("Affitto pagato", m_Columns.m_col_affittopagato);
m_TreeView1->append_column("Riscaldamento pagato", m_Columns.m_col_riscaldamentopagato);
m_TreeView1->append_column("Pagato il", m_Columns.m_col_pagatoil);
m_TreeView1->append_column("Stato", m_Columns.m_col_stato);
m_TreeView1->append_column("Osservazioni", m_Columns.m_col_osservazioni);
m_TreeView1->append_column("Traslocato", m_Columns.m_col_traslocato);
m_TreeView1->append_column("Diversi", m_Columns.m_col_diversi);
m_TreeView1->append_column("Affitto", m_Columns.m_col_affitto);
m_TreeView1->append_column("Posteggio", m_Columns.m_col_posteggio);
m_TreeView1->append_column("Riscaldamento", m_Columns.m_col_riscaldamento);
int i=0;
while ((riga = mysql_fetch_row(res)))
row = *(m_refTreeModel->append());
row[m_Columns.m_col_inqid] = std::stoi(riga[0]);
row[m_Columns.m_col_cognomenome] = riga[1];
row[m_Columns.m_col_stabile] = riga[2];
row[m_Columns.m_col_mese] = std::stoi(riga[3]);
row[m_Columns.m_col_anno] = std::stoi(riga[4]);
row[m_Columns.m_col_posteggiopagato] = std::stod(riga[5]);
row[m_Columns.m_col_affittopagato] = std::stod(riga[6]);
row[m_Columns.m_col_riscaldamentopagato] = std::stod(riga[7]);
row[m_Columns.m_col_pagatoil] = riga[8];
row[m_Columns.m_col_stato] = riga[9];
row[m_Columns.m_col_osservazioni] = riga[10];
row[m_Columns.m_col_traslocato] = riga[11];
row[m_Columns.m_col_diversi] = std::stod(riga[12]);
row[m_Columns.m_col_affitto] = std::stod(riga[13]);
row[m_Columns.m_col_posteggio] = std::stod(riga[14]);
row[m_Columns.m_col_riscaldamento] = std::stod(riga[15]);
if(i%2 == 0)
row[m_Columns.m_coloreriga] = Gdk::RGBA("#ff0000");
else
row[m_Columns.m_coloreriga] = Gdk::RGBA("#cccccc");
i++;
mysql_free_result(res);
mysql_close(connection);
【讨论】:
很高兴你做到了。恭喜:)以上是关于GtkTreeView 交替行颜色的主要内容,如果未能解决你的问题,请参考以下文章