Filename of image in event box - c++

I'm pretty new to GTK3.
I've created an array of EventBoxes and each of them contains an image. All boxes are linked to the same on_button_pressed method. When the user clicks an image, I'd like to get the file name of the image displayed in the event box. Is there any way this can be done?
Thanks for all the help.
---- EDIT 1 -------------------------
Code:
//In my header file, I've declared:
class Window : public Gtk::Window{
//....some other window elements....
Gtk::EventBox portraits[13];
Gtk::Image * portraitImage[13];
};
//In my .cc file, I've implemented:
Window::Window(){
//....creating some window elements ....
for(int i = 0; i < 13; i++){
portraitImage[i] = new Gtk::Image("empty.png");
portraits[i].signal_button_press_event().connect(sigc::mem_fun(*this,
&Window::doOnPortraitClicked));
portraits[i].set_image(*portraitImage[i]);
}
//..........some other code for displaying the window..........//
show_all_children()
}
//doOnPortraitClicked method
bool Window::doOnPortraitClicked(GdkEventButton * button){
?? GValue value = G_VALUE_INIT;
?? g_value_init (&value, G_TYPE_STRING);
?? g_object_get_property(G_OBJECT(button->get_image()), "file", &value);
?? string source = g_value_get_string(&value);
// How do I retrieve "empty.png" from the EventButton?
}
The question marks are lines I can't implement

Related

Qt6: "Unable to read Memory" when pointing to a QLineEdit from a QFormLayout

I want to get the text from a QLineEdit, which is in a QFormLayout, to save it to a File. The saving works fine, but I am not able to get the text form the QLineEdit and when I look at it from the Debugger it says "Unable to read Memory". I canĀ“t figure out how to correctly point to the QLineEdit, so that I can get the text.
With this code I want to get the text:
QJsonArray Kegelbuch::saveSettings() {
QFormLayout* formLayout = (QFormLayout*)ui.einstellungenTab->layout();
QJsonArray data;
QLineEdit* settingsEdit;
for (int i = 0; i < formLayout->rowCount(); i++) {
settingsEdit = (QLineEdit*)formLayout->itemAt(i, QFormLayout::ItemRole::FieldRole);
}
return data;
}
How the window looks:
Replace
settingsEdit = (QLineEdit*)formLayout->itemAt(i, QFormLayout::ItemRole::FieldRole);
with
settingsEdit = (QLineEdit*)(formLayout->itemAt(i, FormLayout::ItemRole::FieldRole)->widget());
Background: itemAt() returns a QLayoutItem*, so you need to call QWidget *QLayoutItem::widget() to get the widget.

How to access ui from main window in another qdialog?

I am having trouble accessing a QTextEdit from a main window in another form. Please help.
void properties::on_okWordPushButton_clicked()
{
if (ui->wordcombo->currentText() == "All Words") {
int wordCount = notepad->textEdit->toPlainText().split(QRegExp("(\\s|\\n|\\r)+"), QString::SkipEmptyParts).count();
ui->wordcountlabel->setText(QString::number(wordCount));
}
}
I am getting an error since I cannot read notepad->textEdit
You can use at least 2 possibilities:
Dirty way:
On form creation, pass pointer to your QTextEdit:
// mainwindow.cpp
auto myProperties = new properties(notepad->textEdit);
...
// properties.h
QTextEdit *outerEditor;
// properties.cpp
properties::properties(QTextEdit *editor) {
outerEditor = editor;
...
}
Then, on your slot you can use:
int wordCount = editor->toPlainText().split(QRegExp("(\\s|\\n|\\r)+"), QString::SkipEmptyParts).count();
Qt-way:
Remember - signals/slots are awesome.
Just after form creation, you can connect signal from MainWindow to properties passing text in your QTextEdit and store it locally:
// MainWindow.cpp
auto myProperties = new properties(notepad->textEdit);
connect(this->textEdit, QOverload<QString>::of(&QTextEdit::valueChanged), myProperties, GetNewValue);
// properties.h
void GetNewValue(QString val);
// properties.cpp
void properties::GetNewValue(QString val) {
ui->wordcountlabel->setText(QString::number(val.toPlainText().split(QRegExp("(\\s|\\n|\\r)+"), QString::SkipEmptyParts).count());
}
You can't do this, the ui is a private member of a widget, create a function that returns or sets what you need!

can't remove custom QWidget from QVBoxLayout

This is a reasonably well documented issue but after some reading and trying, I am still struggling to remove a custom widget from a QVBoxLayout.
in the MyClass.h file, I am declaring a vector to hold QPointer objects:
vector< QPointer<MyWidget> > myWidgets;
In the MyClass.cpp file, I am creating the widget and adding it to the layout with:
// create object
void MyClass::createMyWidget() {
QPointer<MyWidget> myWidget(new MyWidget());
myWidgets.push_back(myWidget);
}
//display in layout
void MyClass::displayWidgets() {
for(int i = 0; i < myWidgets.size(); i++) {
myWidgets[i]->setId(i);
ui->myVBoxLayout->addWidget(myWidgets[i].data());
}
}
I have tried the following to remove the widgets from the layout:
void MyClass::removeMyWidget(int id) { // id of the custom widget. id is also position in the layout
for(int i = 0; i < myWidgets.size(); i++) {
if(items[i]->getId() == id) { //correctly targets the widget
// method 1
ui->myVBoxLayout->removeItem(ui->myVBoxLayout->takeAt(i));
//method 2
//ui->myVBoxLayout->removeWidget(items[i]);
}
}
}
Neither of the above works and I have not been able to work out why.
In Qt, a layout is just a manager for sizing and positioning widgets (and other items, like nested layouts or spacers).
When you remove an item from a layout, it is still a child-widget of the parent widget that is layed out by the layout, only that its geometry is no longer managed. Still, the widget is present and displayed, and the user can interact with it.
You also have to remove it or hide it. Try
void MyClass::removeMyWidget(int id){ // id of the custom widget. id is also position in the layout
for(int i=0;i<myWidgets.size();i++){
if(items[i]->getId() == id){ //correctly targets the widget
ui->myVBoxLayout->removeWidget(myWidgets[i]);
delete myWidgets[i]; // <= Delete widget
}
}
}

How to place WT widgets into PDF?

UPDATE
I have also made a post about this issue on the WT forums here http://redmine.webtoolkit.eu/issues/3042. For those of you looking for more information on styling, you will find more information there.
ORIGINAL POST
I have a problem with placing a WGroupBox (eventually a WTable) into a PDF. I am using the PDF sample code from the widget gallery, and have been trying to make it place a WTable I have into the PDF, that being unsuccessful I toned it back to a WGroupBox.
http://www.webtoolkit.eu/widgets/media/pdf-output
The "PDF Images" samples work fine.
The "Rendering HTML to PDF" is where I have problems.
I am using the example exactly as it appears on the page, with the only addition being a WContainerWidget pointer for the items that I want in the PDF.
I do not understand what I need to pass into the renderPdf function as the WString that will be passed into the WPdfRenderer render function, I have tried examining the html of the widget gallery code to compare with my own, and mimic what the gallery is doing, but everything has so far failed to make a PDF.
This is what the URL looks like after I press the "Create Pdf" button, it displays a blank page with an empty pdf "Loading" message at the bottom left.
http://127.0.0.1:8080/ui/report.pdf?wtd=Psx1WfLVpsvVDpxW&request=resource&resource=oylm6i6&rand=1
Sample code below.
//This is the code I have executing on my main page, just a group box with a few labels, a button for the sample pdf, a chart, and another button for the more advanced sample pdf(this one is broken).
//PDF Images sample/////////////////////////////////////////////////////////////////////////
Wt::WContainerWidget* container = table_container;
Wt::WResource* sample_pdf = new SamplePdfResource(container);
Wt::WPushButton* button1 = new Wt::WPushButton("Create pdf sample", container);
button1->setLink(sample_pdf);
//PDF Images sample chart///////////////////////////////////////////////////////////////////
Wt::Chart::WCartesianChart* chart = CreateCartesianChart(table_container);
Wt::WPainter* p = new Wt::WPainter((Wt::WPdfImage*)sample_pdf);
chart->paint(*p);
std::ofstream f("chart.pdf", std::ios::out | std::ios::binary);
sample_pdf->write(f);
//GroupBox to be placed into PDF////////////////////////////////////////////////////////////
Wt::WGroupBox* group_box = new Wt::WGroupBox("Example Group Box", table_container);
group_box->addStyleClass("example");
Wt::WText* text = new Wt::WText("This is a text sample within the group box 1", group_box);
text = new Wt::WText("This is a text sample within the group box 2", group_box);
text = new Wt::WText("This is a text sample within the group box 3", group_box);
//The pdf to take the GroupBox//////////////////////////////////////////////////////////////
Wt::WPushButton* button = new Wt::WPushButton("Create pdf", container);
Wt::WResource* pdf = new ReportResource(group_box, container);
button->setLink(pdf);
Below is the "Rendering HTML to PDF" code from the widget gallery, with the new container widget for a target.
namespace {
void HPDF_STDCALL error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no,
void *user_data) {
fprintf(stderr, "libharu error: error_no=%04X, detail_no=%d\n",
(unsigned int) error_no, (int) detail_no);
}
}
class ReportResource : public Wt::WResource
{
public:
ReportResource(Wt::WContainerWidget* target, Wt::WObject* parent = 0)
: Wt::WResource(parent),
_target(NULL)
{
suggestFileName("report.pdf");
_target = target;
}
virtual void handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
{
response.setMimeType("application/pdf");
HPDF_Doc pdf = HPDF_New(error_handler, 0);
// Note: UTF-8 encoding (for TrueType fonts) is only available since libharu 2.3.0 !
HPDF_UseUTFEncodings(pdf);
renderReport(pdf);
HPDF_SaveToStream(pdf);
unsigned int size = HPDF_GetStreamSize(pdf);
HPDF_BYTE *buf = new HPDF_BYTE[size];
HPDF_ReadFromStream (pdf, buf, &size);
HPDF_Free(pdf);
response.out().write((char*)buf, size);
delete[] buf;
}
private:
Wt::WContainerWidget* _target;
void renderReport(HPDF_Doc pdf)
{
std::stringstream ss;
_target->htmlText(ss);
std::string out = ss.str();
std::string out_id = _target->id();
std::string out_parent_id = _target->parent()->id();
renderPdf(Wt::WString::tr(???), pdf);
}
void renderPdf(const Wt::WString& html, HPDF_Doc pdf)
{
HPDF_Page page = HPDF_AddPage(pdf);
HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
Wt::Render::WPdfRenderer renderer(pdf, page);
renderer.setMargin(2.54);
renderer.setDpi(96);
renderer.render(html);
}
};
//PDF Images code
class SamplePdfResource : public Wt::WPdfImage
{
public:
SamplePdfResource(Wt::WObject *parent = 0)
: Wt::WPdfImage(400, 300, parent)
{
suggestFileName("line.pdf");
paint();
}
private:
void paint()
{
Wt::WPainter painter(this);
Wt::WPen thickPen;
thickPen.setWidth(5);
painter.setPen(thickPen);
painter.drawLine(50, 250, 150, 50);
painter.drawLine(150, 50, 250, 50);
painter.drawText(0, 0, 400, 300, Wt::AlignCenter | Wt::AlignTop, "Hello, PDF");
}
};
//Cartesian chart initialization
Wt::Chart::WCartesianChart* CreateCartesianChart(Wt::WContainerWidget* parent)
{
Wt::WStandardItemModel *model = new Wt::WStandardItemModel(PERFORMANCE_HISTORY, 2, parent);
//Create the scatter plot.
Wt::Chart::WCartesianChart* chart = new Wt::Chart::WCartesianChart(parent);
//Give the chart an empty model to fill with data
chart->setModel(model);
//Set which column holds X data
chart->setXSeriesColumn(0);
//Get the axes
Wt::Chart::WAxis& x_axis = chart->axis(Wt::Chart::Axis::XAxis);
Wt::Chart::WAxis& y1_axis = chart->axis(Wt::Chart::Axis::Y1Axis);
Wt::Chart::WAxis& y2_axis = chart->axis(Wt::Chart::Axis::Y2Axis);
//Modify axes attributes
x_axis.setRange(0, PERFORMANCE_HISTORY);
x_axis.setGridLinesEnabled(true);
x_axis.setLabelInterval(PERFORMANCE_HISTORY / 10);
y1_axis.setRange(0, 100);
y1_axis.setGridLinesEnabled(true);
y1_axis.setLabelInterval(10);
y2_axis.setRange(0, 100);
y2_axis.setVisible(true);
y2_axis.setLabelInterval(10);
//Set chart type
chart->setType(Wt::Chart::ChartType::ScatterPlot);
// Typically, for mathematical functions, you want the axes to cross at the 0 mark:
chart->axis(Wt::Chart::Axis::XAxis).setLocation(Wt::Chart::AxisValue::ZeroValue);
chart->axis(Wt::Chart::Axis::Y1Axis).setLocation(Wt::Chart::AxisValue::ZeroValue);
chart->axis(Wt::Chart::Axis::Y2Axis).setLocation(Wt::Chart::AxisValue::ZeroValue);
// Add the lines
Wt::Chart::WDataSeries s(1, Wt::Chart::SeriesType::LineSeries);
chart->addSeries(s);
//Size the display size of the chart, has no effect on scale
chart->resize(300, 300);
return chart;
}
The pdf renderer takes a HTML string. So I think the solution is
renderPdf(WString::fromUTF8(out), pdf);

GTKmm - How to put a pixbuf in a treeview

I'm learning how to use GTKmm and I'm having a really hard time figuring out how to put an image into a treeview. I used Glade to create a treestore with 3 columns, one of which is a GdkPixbuf called store_pixbuf. I also created a treeview in glade, with a column that has both a pixbuf cell renderer called int_col_pict and a char array cell renderer. In my code, I have the usual MyColumns definition for the treestore like:
class MyModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
Gtk::TreeModelColumn<Glib::ustring> store_hostname;
Gtk::TreeModelColumn<Glib::ustring> store_intname;
Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > store_pict;
MyModelColumns () { add(store_hostname); add(store_intname); add(store_pict);}
};
and use the following bit of code to populate it.
//Get a pointer to the treestore
Glib::RefPtr<Gtk::TreeStore> treestore = Glib::RefPtr<Gtk::TreeStore>::cast_static(builder->get_object("routerTreeStore"));
//make sure the pointer isn't bad
if(treestore){
MyModelColumns columns;
//populate the first column
Gtk::TreeRow row= *(treestore->append());
row[columns.store_hostname] = router->hostname;
//populate all children
for(int i=0; i<router->interfaces.size(); i++)
{
//append child row
Gtk::TreeRow child = *(treestore->append(row.children()));
//insert data into the row
child[columns.store_pict] = Gdk::Pixbuf::create_from_file("red_dot.png");
child[columns.store_intname] = router->interfaces[i].interfaceName;
}
}//if
I initially tried to use a stock image, but I could not figure out what function I was supposed to use, so then I tried to use Gdk::Pixbuf::create_from_file() (as you can see above), but at run time I get the following error:
Gtk-WARNING **: gtktreestore.c:765: Unable to convert from GdkPixbuf to gtkmm__GdkPixbuf
Click here to see what it looks like running. The image is supposed to go on the same line as the "FastEthernet..." lines
Does anyone know how I can solve this? Am I going about it completely wrong? Thanks for looking, every little bit of help is appreciated!
Your code looks correct. I just coded up a quick example and with gtkmm-2.4, I have no problems creating a column for a Glib::RefPtr
I couple of questions: what version of gtkmm are you using? Are you adding a column in the treeview for the Pixbuf?
I won't post my complete example but the relevant bits are:
in example.h
//Tree model columns:
class ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns()
{ add(m_col_store_pict);}
Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > m_col_store_pict;
};
ModelColumns m_Columns;
in example.cpp
//Create the Tree model:
m_refTreeModel = Gtk::ListStore::create(m_Columns);
m_TreeView.set_model(m_refTreeModel);
//Fill the TreeView's model
Gtk::TreeModel::Row row = *(m_refTreeModel->append());
row[m_Columns.m_col_store_pict] = Gdk::Pixbuf::create_from_file("/usr/share/icons/gnome/22x22/apps/arts.png");
row = *(m_refTreeModel->append());
row[m_Columns.m_col_store_pict] = Gdk::Pixbuf::create_from_file("/usr/share/icons/gnome/22x22/apps/fonts.png");
row = *(m_refTreeModel->append());
row[m_Columns.m_col_store_pict] = Gdk::Pixbuf::create_from_file("/usr/share/icons/gnome/22x22/apps/access.png");
//Add the TreeView's view columns:
m_TreeView.append_column("Some Picture", m_Columns.m_col_store_pict);
Is that any help?
If all you want is a potentially different stock icon for each row, you can do it by setting an pixbuf renderer attribute. The idea is that the column contains a string stock-id, which the renderer displays as an icon.
// in your class declaration
struct columnsRecord : public Gtk::TreeModel::ColumnRecord {
...
Gtk::TreeModelColumn<std::string> stockID; // stock id name
...
} treeColumns;
Gtk::CellRendererPixBuf pixBufRenderer;
...
// setting up columns
int numcols = treeView.append_column("Icon Column", pixBufRenderer); // returns # cols after append
treeView.get_column(numcols-1)->add_attribute(pixBufRenderer,"stock-id",treeColumns.stockID)
...
// setting a row
std::string id = good_val ? Gtk::Stock::YES.id : Gtk::Stock::NO.id;
rowiter->set_value(columns.stockID,id);
Looks like it's necessary to explicitly set CellRenderer for pixbuf cells.
This code snippet displays CANCEL icon on each data row.
Gtk::CellRendererPixbuf *cross = Gtk::manage(new Gtk::CellRendererPixbuf());
cross->property_stock_id() = Gtk::StockID(Gtk::Stock::CANCEL).get_string();
cross->property_stock_size() = Gtk::ICON_SIZE_BUTTON;
[...]
int cols_count = m_TreeView.append_column("icons", *cross);
For displaying custom images you need to remove two lines setting stock_id add something like this below:
Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count - 1);
if(pColumn) {
pColumn->add_attribute(cell->property_value(), columns.store_pict);
}