GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Nov 26, 2014 9:28 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Displaying Text in GtkTextView
PostPosted: Mon Jul 07, 2014 9:09 pm 
Offline
Familiar Face

Joined: Mon Jul 07, 2014 7:55 pm
Posts: 5
Hello!
First of all, I am a true beginner at using GTK, so I apologize in advance if the questions I am asking are very rudimentary.

I am using GTK with Anjuta. So what I want to do is to display text in the GtkTextView box when a button is clicked. I created a GUI using Glade and created buttons and the TextView box as GtkWidgets. A screenshot of the GUI is shown below:

Image

When the GUI is created, a struct is made which defines the different components the make up the GUI:

Code:
/* ANJUTA: Macro MENGTEST_APPLICATION gets Mengtest - DO NOT REMOVE */
struct _MengtestPrivate
{
   /* ANJUTA: Widgets declaration for mengtest.ui - DO NOT REMOVE */
   GtkWidget* entrybuffer2;
   GtkWidget* comboboxtext4;
   GtkWidget* comboboxtext3;
   GtkWidget* comboboxtext2;
   GtkWidget* comboboxtext1;
   //GtkWidget* entrybuffer1;
   GtkWidget* SendB;
   GtkWidget* gtk_databox2;
   GtkWidget* progressbar1;
   GtkWidget* ReadB;
   GtkWidget* ConnectB;
   
   GtkWidget* StartScanB;
   GtkWidget* StopScanB;
   
   GtkWidget* textview1;   //I added this here myself because I assumed that if I created a new Widget, it would have to be declared here (and the ID for the text view is set as "textview1")
   //GtkTextBuffer* textbuffer1;
};


To initialize these widgets Anjuta also created the following code:

Code:
/* ANJUTA: Widgets initialization for mengtest.ui - DO NOT REMOVE */
   priv->entrybuffer2 = GTK_WIDGET (gtk_builder_get_object(builder, "entrybuffer2"));
   priv->comboboxtext4 = GTK_WIDGET (gtk_builder_get_object(builder, "comboboxtext4"));
   priv->comboboxtext3 = GTK_WIDGET (gtk_builder_get_object(builder, "comboboxtext3"));
   priv->comboboxtext2 = GTK_WIDGET (gtk_builder_get_object(builder, "comboboxtext2"));
   priv->comboboxtext1 = GTK_WIDGET (gtk_builder_get_object(builder, "comboboxtext1"));
   //priv->entrybuffer1 = GTK_WIDGET (gtk_builder_get_object(builder, "entrybuffer1"));
   priv->SendB = GTK_WIDGET (gtk_builder_get_object(builder, "SendB"));
   priv->gtk_databox2 = GTK_WIDGET (gtk_builder_get_object(builder, "gtk_databox2"));
   priv->progressbar1 = GTK_WIDGET (gtk_builder_get_object(builder, "progressbar1"));
   priv->ReadB = GTK_WIDGET (gtk_builder_get_object(builder, "ReadB"));
   priv->ConnectB = GTK_WIDGET (gtk_builder_get_object(builder, "ConnectB"));

   priv->StartScanB = GTK_WIDGET (gtk_builder_get_object(builder, "StartScanB"));
   priv->StopScanB = GTK_WIDGET (gtk_builder_get_object(builder, "StopScanB"));
   priv->textview1 = GTK_WIDGET (gtk_builder_get_object(builder, "textview1"));  //added this myself
   //priv->textbuffer1 = GTK_TEXT_BUFFER (gtk_builder_get_object(builder, "textbuffer1"));


Now, what I want to do is display text when the "Start Scan" button is pressed. Thus, when the GUI is created, the ID for the "Start Scan" button is "StartScanB" and for the Signals, when the GtkButton is clicked, it calls the handler "doStartScan". The following is the code in the handler "doStartScan":

Code:
gboolean doStartScan(gpointer data)
{
  GtkTextBuffer *textbuffer1;
  priv->textview1 = gtk_text_view_new ();
  textbuffer1 = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->textview1));
  gtk_text_buffer_set_text (textbuffer1, "Hello, this is some text", -1);
   
  return 0;
}


Now, when I run the code and click the "Start Scan" button, nothing appears in the Text View.

I have also tried adding "textbuffer1" as a buffer for the TextView (under Text View Attributes), but the text still does not appear.

Image



Does anyone have an idea of what I am doing wrong? Thank you in advance for your help!


Top
 Profile  
 
 Post subject: Re: Displaying Text in GtkTextView
PostPosted: Mon Jul 14, 2014 4:10 pm 
Offline
Familiar Face

Joined: Fri Nov 28, 2008 8:48 pm
Posts: 5
hi, why you reinitialize the textview? try to wacth the menory at priv->textview1, is it null?
Code:
...
gboolean doStartScan(gpointer data)
{
  GtkTextBuffer *textbuffer1;
  textbuffer1 = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->textview1));
  gtk_text_buffer_set_text (textbuffer1, "Hello, this is some text", -1);
   
  return 0;
}
...


Top
 Profile  
 
 Post subject: Re: Displaying Text in GtkTextView
PostPosted: Mon Jul 14, 2014 6:39 pm 
Offline
Familiar Face

Joined: Mon Jul 07, 2014 7:55 pm
Posts: 5
Thanks! That did the trick!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group