Usually in our Scope design, we can make a request in a Query by searching. In fact, we can also use a preview button to make a Query request and generate the display results. In today’s article, we’ll show how a request happens in Preview.
\
1) Type of request
\
We can use the following two apis in “CannedQuery” to produce the desired query request:
-
set_query_string (std::string const &query_str)\
-
set_user_data (Variant const &value)\
The difference between the two apis is that the previous API can search by typing the desired string into the search box. In the case of set_user_data, the search string is already in the passed parameters, which we can parse directly and make parsed parameters part of our API request. In this case, we usually don’t need to search any more.
\
Let’s show how to use it through practical examples.
\
\
2) set_query_string
\
In our Preview, we add the following code:
\
preview.cpp
\
sc::CannedQuery new_query(SCOPE_INSTALL_NAME);
std::string str("carousel");
new_query.set_query_string(str);
builder1.add_tuple({
{"id", Variant("user_videos")},
{"label", Variant("Show query string")},
{"uri", Variant(new_query.to_uri())}
});
Copy the code
\
As you can see from the above code, we pre-set a string “carousel” in query. In our query, we identify this case with the following code:
query.cpp
\
void Query::run(sc::SearchReplyProxy const& reply) { try { // Let's first check the query string const sc::CannedQuery &query(sc::SearchQueryBase::query()); // Get the query string string query_string = query.query_string(); QString queryStr = QString::fromStdString(query_string); qDebug() << "query: " << queryStr; if ( queryStr.startsWith("carousel") ) { qDebug() << "it is from the preview"; int count = images_.count(); auto cat = reply->register_category( "Carousel", "Carousel" , "", sc::CategoryRenderer(CAT_RENDERER9) ); for ( int i = 0; i < count; i ++ ) { pushResult( reply, &cat, i); }}... }Copy the code
In the above code, we can match the string with carousel, and if the request del starts with this string, we do the separate processing. Run our Scope:
\
\
\
When we click “Show Query String” in Preivew, our query displays the image shown on the right. Display the word “Carousel” in the search box. And display the carousel image we need. We can also enter any string we want in the input box, as long as the first few strings are “carousel”.
\
\
3) set_user_data
\
In our Preview, we write the following code:
\
preview.cpp
\
sc::CannedQuery new_query1(SCOPE_INSTALL_NAME);
new_query1.set_user_data(Variant("num=" + std::to_string(4)));
builder1.add_tuple({
{"id", Variant("user_videos")},
{"label", Variant("User data")},
{"uri", Variant(new_query1.to_uri())}
});
Copy the code
In this case, we write the search code directly into the search parameters, which we can parse in the query and display the results:
query.cpp
\
void Query::run(sc::SearchReplyProxy const& reply) { try { // Let's first check the query string const sc::CannedQuery &query(sc::SearchQueryBase::query()); // Get the query string string query_string = query.query_string(); QString queryStr = QString::fromStdString(query_string); qDebug() << "query: " << queryStr; if (sc::SearchQueryBase::query().has_user_data()) { string userdata = sc::SearchQueryBase::query().user_data().get_string(); qDebug() << "it has user data in it"; cerr << "user_data: " << userdata << endl; const std::string prefix("num="); if (! userdata.compare(0, prefix.size(), prefix)) { qDebug() << "prefix is found!" ; std::string num = userdata.substr(prefix.size()); cerr << "num: " << num << endl; int number = num.back() - '0'; qDebug() << "number: " << number; // Let's display a vertical-journal according to the number auto cat = reply->register_category( "vertical-journal", "vertical-journal" , "", sc::CategoryRenderer(CAT_RENDERER12) ); for ( int i = 0; i < number; i ++ ) { pushResult( reply, &cat, i); }}}... }Copy the code
Above, we check for user_data. If so, let’s do it separately. The difference between this case and the one above is that we don’t need to enter any additional parameters for our search, because we can parse user_data to get all the parameters needed for the search. Run our scope:
\
\
\
From above, we can click the “User Data” button to go directly to our Query interface. We don’t need to enter any parameters. The four grid images we entered are currently displayed.
\
Our entire project source at: github.com/liu-xiao-gu…