Passing values through links in QT - c++

I'm writing a program in C++ and qt4 that is supposed to generate a various number (hundreds )of clickable links in a QTextBrowser depending on data from input files.
The idea is that when the user clicks one of these links a value will be passed to a function named 'on_QTextBrowser_anchorClicked(QUrl)'.
I have created a QTextBrowser which displays HTML code and I manage to create different links to every element added. The problem lies within passing the Url defined in href="URL" to the QUrl.
When setOpenLinks for the QTextBrowser is "true", and I print the URL I get the correct result. But I can't pass this URL (which is a value and not a real URL) to a function.
When setOpenLinks is "false", the anchorClicked(Url) function passes "" as the Url and here I would like to have the URL printed when setOpenLinks=true.
How can I achieve this? Is there a better way (probably is) to connect a various number (approx. between 50-1000) of generated links to a function using QTextBrowser.
My code:
Compassindex.cpp
void CompassIndex::on_seqBrowser_anchorClicked(QUrl input)
{
QString Input = input.toString();
QByteArray nByte = Input.toUtf8();
std::cerr<<Input.data(); //Print Url on the screen to ensure value is passed
}
void CompassIndex::readfile()
{
QString Line;
Int number_out=0;
... //Imports data that will be printed in the QTextBrowser
... //loop for creating links for n number of elements
Line="<a href=\"";
Line+=number_out; //the value (fake Url) that I want to pass to anchorClicked()
Line+="\">";
Line+=nameArr[n]; //inserts the name for the link
Line+="</a>";
number_out++;
...
ui->seqBrowser->insertHtml(Line);
... //end of loop
}
Many thanks for your reply!

I'm using QTextBrowser to provide an actionable user interface for pqConsole. You can read more about in this answer.
I pass around Prolog invocations, and all seems to be working. My anchorClicked slot is as simple as
void ConsoleEdit::anchorClicked(const QUrl &url) {
query_run(url.toString());
}
Note I also have defined (as dummy)
void ConsoleEdit::setSource(const QUrl &name) {
qDebug() << "setSource" << name;
}
I don't touch setOpenLinks(), that defaults to true according to the docs.

Related

Saving data from SQLite data base into a Variable

I'm trying to save the username from the database into a variable to use in another QDialog, but the variable is blank when I try to use it in the other dialog
qry.prepare("select nome from Users where email='"+user+"' and senha='"+password+"'");
if(qry.exec()) {
int count=0;
while(qry.next()) {
ui->label_2->setText(qry.value(0).toString());
count++;
}
if (count == 1) {
usuario = ui->label_2->text();
ui->errologin->setText("");
logado= true;
this->hide();
}
This is how i'm trying to call it in the other Dialog:
ui->label->setText(log.usuario);
And this ist the variable declaration in the header file:
public:
explicit login(QWidget *parent = nullptr);
~login();
bool logado;
QString usuario;
There is one thing that you could do better, instead of this:
qry.prepare("select nome from Users where email='"+user+"' and senha='"+password+"'");
Replace it by:
qry.prepare("select nome from users where email=? and senha=?");
qry.bindValue(0, user);
qry.bindValue(1, password);
Please, don't build your queries concatenating strings.
Other than that, there is no way to tell where is your mistake, because you don't provide a minimal reproducible example, as #AlanBirtles and #eyllanesc have requested. I don't see the problem in the code you provided, it may be somewhere hidden in the code that you don't show.
Another advice: trying to reduce your problem to a minimal test program may help you to find the problem by yourself. For instance, if you doubt about your usage of the QSqlQuery class, then put your query in a console program and see if it returns the correct results. If it still doesn't work right, then post your (full) test program here in SO.

QUrl doesn't work with parts from text file (Qt c++)

I want to make an url with some parts extracted from a text file.
I have the following code:
crack_order = new QFile("orders.txt"); //I have QFile *crack_order in headers
crack_order->open(QIODevice::ReadOnly);
while(!crack_order->atEnd()){
QStringList orders;
orders = QString(crack_order->readAll()).split('\n',QString::SkipEmptyParts);
foreach (QString order, orders){
ui->debugtextbox->setText(order); //until here, it works fine!
url = "http://www.example.com/customers/orders/"+order+"/another/thing.txt";
it does not work, the url request doesn't performed correctly. HOWEVER, if I put the following variable:
"QString order = "12233";
before "url", it works!
Then, I guess the problem is that it's getting the "order" from a text file, and QUrl doesn't like it.
Any idea?
Thanks in advanced.

How can I change the Print Spooler filename/document name associated with wxPrintout in wxwidgets

I have managed to attach my filename to my print out by overloading the wxPrintout constructor with my chosen name.
wxPrintout(const wxString& title = wxT("Printout"));
In my code:
VRPrintout::VRPrintout(blababala):wxPrintout(_("Heartrate")),
But on printing to save as a PDF, in the filename dialog, it reads "Printing Heartrate".
How can I get rid of "Printing" added before the name or even change it?
#Credits to Xaviou from wxDev.fr.
You can try overriding the "OnBeginDocument" method in your wxPrintout derived class.
The default code is the following (the "OnBeginDocument" is virtual) in wx-src/common/prntbase.cpp:584 :
bool wxPrintout::OnBeginDocument(int WXUNUSED(startPage), int WXUNUSED(endPage))
{
return GetDC()->StartDoc(_("Printing ") + m_printoutTitle);
}
I think that if you provide yours with the correct text, it should be ok.
Regards
Xav'
Edit : Tested on an app of mine that do printing stuff, and it works...
Worked for me too

Word automation with C++ Builder 5

I'm trying to control Word through a c++ builder 5 application. I would like to
open a ".dot" model file created with Word and modify it. In the ".dot" model file
there are some fields. For example, Title, LastName, FirstName, Address
and so on, and I would like to modify these fields putting text into them and then
saving file with a new name, for example "Warning.doc" leaving the ".dot" file
unaltered.
I can open the file, count the number of fields it contains, but then
when it comes to replacing each field with a string I don't know how to do because
I don't have a complete documentation on OleFunction and OlePropertyGet methods. I attach my source code to this message, can anybody help me to solve this problem please?
try
{
my_word = Variant::CreateObject("word.application");
}
catch (...)
{
Application->MessageBox("Unable to obtain Word automation object",
"Error:",MB_OK | MB_ICONERROR);
}
my_word.OlePropertySet("Visible", (Variant)true);
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Variant this_doc;
Variant my_fields;
Variant test;
int k,field_count;
AnsiString test1;
AnsiString filename = "d:\\ProgrammaWord\\1-Avviso.dot";
my_docs = my_word.OlePropertyGet("Documents");
this_doc = my_docs.OleFunction("Open", filename);
my_fields = this_doc.OlePropertyGet("Fields");
field_count = my_fields.OlePropertyGet("Count");
for(k = 1; k <= field_count; k++)
{
test = my_fields.OleFunction("Item",(Variant)k);
test1 = test.OleFunction("Value"); //This instruction throws an exception
// "Value" is not a recognized parameter
// in this case
Memo1->Lines->Add(test1);
}
}
I never used word Ole but I used it for Outlook and Excel, I can't try it with word since I'm currently on OSX but you should try something similar as what I did.
The generic way of using Ole was to OleGetproperty() while you get the targeted field then OleSetProperty("action", ...).
for example when I wanted to change the color of the text in a particular cell of my excel document I used:
Variant _excel = Variant::CreateObject("Excel.Application");
Variant _workbook = _excel.OlePropertyGet("WorkBooks").OleFunction("Open", filename);
Variant _worksheet = _workbook.OlePropertyGet("WorkSheets", sheet);
_worksheet.OlePropertyGet("Cells", row, col).OlePropertyGet("Font").OlePropertySet("Color", color);
Here I instanciate an excel object, then I load a file into it (_workbook), then I select the _worksheet from the _workbook and I start my business.
Here comes the interesting part:
It concist of getting to a particular cell, getting the font object from it, and then setting the color of this font object.
Disclaimer: This is an example from my sources for excel, it's not directly related to your example, but maybe you can understand the principe with it. I can't try to figure out what you need because I have no windows right now.
Hope this can help you. Finding ressources for OLE can be fierce if you don't have the good patterns to look for.

Qt - Download Images from web

I am trying to write an application using Qt/C++ to download images from web. The program will be provided a list of URLs as a text file, and should open each URL and download all images in each website.
In order to do so, I subclass QNetworkAccessManager and override createRequest method/function like below:
QNetworkReply* clsNAM::createRequest(Operation op,const QNetworkRequest & req,
QIODevice * outgoingData )
{
return new clsNR(QNetworkAccessManager::createRequest(_op,_req,_outgoingData));
}
My subclass of QNetworkReply class, clsNR, identifies the type of the reply from its raw header, something like:
bool clsNR::isImage()
{
if(this->rawHeader("Content-Type").toLower().startsWith("image"))
return true;
else
return false;
}
As I get readyRead signal (which means there is data available to read) ,I store the coming in a QbyteArray. I have connected a slot in clsNR to save the image when the QNetworkReply is finished. It looks like:
void clsNR::slotCloseConnection()
{
if(isImage())
{
QImage image;
bool bool1 = image.loadFromData(ContentData);
QString name(QDir::homePath());
name.append(this->url().path());
bool bool2 = image.save(name,0,-1);
}
}
There is something wrong with my saving. bool1 is true, but bool2 is false. The reason I'm using the url path for the name is to generate unique name for images. When I print out the name, it's something like:
"/home/guest/images/splash/3.jpg"
So My questions would be:
What I'm doing wrong with QImage?
Can I use QFile and QStream to write down the images instead?
I'll add something else, like Mat said in your comment. If you need to check if the directory exists use Qdir::exists() and if you need to create it use QDir::mkdir()