|
MediasharpDE
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

8. August 2012 13:54
Hallo,
ich habe eine Klasse namens Song, die bisher nur die Eigenschaft Titel hat. Nun möchte ich ein TreeView mit Song Elementen befüllen. Der Titel soll dann angezeigt werden. Hier mein bisheriger Code. SongRenderer.vala:
using Gtk;
public class SongRenderer : CellRendererText {
static Song s;
// Constructor
public SongRenderer () {
}
public override void get_size (Widget widget, Gdk.Rectangle? cell_area,
out int x_offset, out int y_offset,
out int width, out int height)
{
x_offset = 0;
y_offset = 0;
width = -1;
height = -1;
}
public override void render (Cairo.Context ctx, Widget widget,
Gdk.Rectangle background_area,
Gdk.Rectangle cell_area,
CellRendererState flags)
{
Gdk.cairo_rectangle (ctx, background_area);
text = s.Title;
base.render(ctx, widget, background_area, cell_area, flags);
}
}
Song.vala
public class Song : GLib.Object {
public string Title;
// Constructor
public Song (string title) {
this.Title = title;
}
}Und mein Code zum TreeView:
ManagerStore = new ListStore(1, typeof(Song));
ManagerTree.insert_column_with_attributes (-1, "Songs", new SongRenderer (), "text" , 0);
ManagerStore.append(out managerIter);
ManagerStore.set(managerIter, 0, lied);
Wie mache ich es, dass der Titel angezeigt wird? LG
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 09:49
|
|
Fanatics
Anmeldungsdatum: Aug. 25, 2010
Beiträge: 520
|

9. August 2012 09:56
Eins vorweg: Ich kenne die Sprache nicht, die Du da programmierst, ich habe aber Erfahrung mit Java und habe dort auch schon Oberflächen programmiert. Ich vermute Dein Problem im Renderer, der nämlich zu keiner Zeit eine Referenz auf Dein Songobjekt erhält. Ich würde im Konstruktor des Renderers den Song übergeben, damit der Renderer auch was hat, was er rendern soll. Grüße Fanatics
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 10:18
Ja, aber der Renderer wird ja nur bei der Erstellung der Spalte aufgerufen.
|
|
Fanatics
Anmeldungsdatum: Aug. 25, 2010
Beiträge: 520
|

9. August 2012 10:26
Und wann bekommt er die Referenz auf den Song? Welchen Titel soll er den rendern? Gdk.cairo_rectangle (ctx, background_area);
text = s.Title; Du hast zu keiner Zeit den Song an den Renderer übergeben, oder Du hast nicht den gesamten Code gepostet... versuche: public SongRenderer (Song s) {
this.s=s;
}
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 11:47
Das Problem ist ja, dass der Renderer ja nicht von mir aufgerufen wird, wenn ich ein Objekt hinzufüge, sondern beim Erstellen der Spalte:
Spalte wird erstellt:
ManagerTree.insert_column_with_attributes (-1, "Songs", new SongRenderer(), "text" , 0); Song wird hinzugefügt:
ManagerStore.append(out managerIter);
ManagerStore.set(managerIter, 0, thesong); beim Song hinzufügen wird ja soweit ich das verstehe kein Renderer von mir initialisiert...
|
|
Fanatics
Anmeldungsdatum: Aug. 25, 2010
Beiträge: 520
|

9. August 2012 12:22
ok, lass mich nochmal anders fragen: Mit welcher Methode wird
static Song s initialisiert?
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 14:48
Mit gar keiner. Ich weis ja nicht wie. Aus den CellRendererSamples bin ich nicht schlau geworden
|
|
diesch
Supporter
Anmeldungsdatum: Feb. 18, 2009
Beiträge: 4130
Wohnort: Freiburg
|

9. August 2012 15:03
Die Song-Variable in SongRenderer muss eine Property sein, deren Namen du beim Aufruf von insert_column_with_attributes statt text angibst.
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 15:05
Wie erstelle ich eine solche Property?
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 15:06
Ganz einfach so? private int age = 32;
public int get_age() {
return this.age;
}
public void set_age(int age) {
this.age = age;
}(genommen aus dem Vala Tutorial von gnome.org)
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 15:12
Der Code ist dann folgender: soundtube.vala:
using GLib;
using Gtk;
using TagLib;
using Gst;
public class Main : GLib.Object
{
/*
* Uncomment this line when you are done testing and building a tarball
* or installing
*/
//const string UI_FILE = Config.PACKAGE_DATA_DIR + "/" + "soundtube.ui";
const string UI_FILE = "src/soundtube.ui";
/* ANJUTA: Widgets declaration for soundtube.ui - DO NOT REMOVE */
Gtk.MenuItem info_mnu_itm;
Gtk.Window aboutdialog;
Gtk.Button close_about;
Gtk.Toolbar tbr_main;
Gtk.Box ManagerBox;
Gtk.Box FunctionsBox;
Gtk.TreeView ManagerTree;
Gtk.ListStore ManagerStore;
Gtk.TreeIter managerIter;
public Main ()
{
try
{
var builder = new Builder ();
builder.add_from_file (UI_FILE);
builder.connect_signals (this);
info_mnu_itm = builder.get_object ("info_mnu_itm") as Gtk.MenuItem;
info_mnu_itm.activate.connect(show_about);
var window = builder.get_object ("window") as Window;
FunctionsBox = builder.get_object ("FunctionsBox") as Gtk.Box;
ManagerBox = builder.get_object ("ManagerBox") as Gtk.Box;
aboutdialog = builder.get_object ("aboutwindow") as Window;
close_about = builder.get_object ("closeabout") as Gtk.Button;
tbr_main = builder.get_object("tbr_main") as Gtk.Toolbar;
ManagerTree = new Gtk.TreeView();
ManagerStore = new ListStore(1, typeof(string));
ManagerTree.set_model(ManagerStore);
ManagerTree.insert_column_with_attributes (-1, "Songs", new CellRendererText(), "thesong" , 0);
Song song = new Song("Titel", "Interpret", "Album", 10, "test");
ManagerStore.append(out managerIter);
ManagerStore.set(managerIter, 0, song);
ManagerBox.pack_start(ManagerTree, true, true, 0);
tbr_main.get_style_context().add_class(STYLE_CLASS_PRIMARY_TOOLBAR);
close_about.clicked.connect(close_about_mtd);
/* ANJUTA: Widgets initialization for soundtube.ui - DO NOT REMOVE */
window.show_all ();
}
catch (Error e) {
stderr.printf ("Could not load UI: %s\n", e.message);
}
}
[CCode (instance_pos = -1)]
public void on_destroy (Widget window)
{
Gtk.main_quit();
}
private void show_about() {
aboutdialog.show_all();
}
private void close_about_mtd() {
aboutdialog.hide();
}
static int main (string[] args)
{
Gtk.init (ref args);
Gst.init (ref args);
var app = new Main ();
Gtk.main ();
return 0;
}
}
Song-renderer.vala /* -*- Mode: vala; tab-width: 4; intend-tabs-mode: t -*- */
/* soundtube
*
soundtube is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* soundtube is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Gtk;
public class SongRenderer : CellRendererText {
public Song thesong;
public Song get_song() {
return this.thesong;
}
public void set_age(Song song) {
this.thesong = song;
}
// Constructor
public SongRenderer () {
}
public override void get_size (Widget widget, Gdk.Rectangle? cell_area,
out int x_offset, out int y_offset,
out int width, out int height)
{
x_offset = 0;
y_offset = 0;
width = -1;
height = -1;
}
public override void render (Cairo.Context ctx, Widget widget,
Gdk.Rectangle background_area,
Gdk.Rectangle cell_area,
CellRendererState flags)
{
Gdk.cairo_rectangle (ctx, background_area);
text = thesong.Title;
base.render(ctx, widget, background_area, cell_area, flags);
}
}
Song.vala
/* -*- Mode: vala; tab-width: 4; intend-tabs-mode: t -*- */
/* soundtube
*
soundtube is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* soundtube is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class Song : GLib.Object {
public string Title;
public string Artist;
public string Album;
public int Length;
public string Uri;
public Song (string title, string artist, string album, int length, string uri) {
this.Title = title;
this.Artist = artist;
this.Album = album;
this.Length = Length;
this.Uri = uri;
}
}
Das gibt folgenden Error und es erscheint nichts:
Gtk-WARNING **: Cannot connect attribute `thesong' for cell renderer class `GtkCellRendererText' since attribute does not exist
|
|
diesch
Supporter
Anmeldungsdatum: Feb. 18, 2009
Beiträge: 4130
Wohnort: Freiburg
|

9. August 2012 15:20
Ich kann kein Vala, aber ich vermute mal, ja.
|
|
diesch
Supporter
Anmeldungsdatum: Feb. 18, 2009
Beiträge: 4130
Wohnort: Freiburg
|

9. August 2012 15:22
In insert_column_with_attributes musst du statt ´CellRendererText SongRenderer benutzen, damit dein CellRenderer auch benutzt wird.
|
|
MediasharpDE
(Themenstarter)
Anmeldungsdatum: Feb. 20, 2012
Beiträge: 106
|

9. August 2012 15:28
Da kommt der gleiche Error, nur halt mit SongRenderer.
|